Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader;  
public class Coba3 {
 public static void main(String[] args) {  
     BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); 
     String[][] dataMhs = new String[5][3]; 
     System.out.println("Input Data student"); 
     for(byte i=0; i<dataMhs.length; i++) { try { 
     System.out.println("Data to-"+(i+1)); 
     System.out.print("Id : "); 
     String input = dataIn.readLine(); 
     dataMhs[i][0] = input;
     System.out.print("Name : "); 
     input = dataIn.readLine();
     dataMhs[i][1] = input;
     System.out.print("Age : ");
     input = dataIn.readLine(); 
     dataMhs[i][2] = input; 
     System.out.println(); }
     catch (IOException e) 
     {  System.out.println("Error in getting input"); } }  
   
System.out.println("Display student Data "); 
System.out.println("=======================");
for(byte i=0; 
 i<dataMhs.length; i++)
 
{ System.out.println("Data To-"+(i+1));
System.out.println("Id:"+dataMhs[i][0]);  
System.out.println("Name : "+dataMhs[i][1]);
System.out.println("Age : "+dataMhs[i][2]); 
System.out.println(); } 
} 
}    


What I have tried:

How to display index with oldest age only
Posted
Updated 23-Dec-20 5:02am
v2
Comments
Sandeep Mewara 23-Dec-20 9:27am    
It would help you if you explain a more on what are you trying to do.
Rizky Feriansyah 23-Dec-20 9:36am    
I'm trying to make a program to show the oldest age(umur) of the input and output display (umur) oldest age
Patrice T 23-Dec-20 10:43am    
Try to give some context. What the program is doing.
Remember, we don't know what you talk about.
Richard MacCutchan 23-Dec-20 11:02am    
You could start by tidying up the indentations and blocks in your code so it is clearly readable. You then need to explain what you mean by "oldest age". As far as I can see you are not capturing age values as numeric types so it is difficult to do valid comparisons on them.

1 solution

Start by indenting your code, so it's vaguely readable - at the moment it's very hard to work out what is going on because none of your indentation reflects the structure of your actual processing.

Then think about what exactly you are trying to do: at the moment all you do is read in the data (hoping that there is exactly the right amount, but not doing anything about failures except report them and leave rubbish in a location).

I'd start by creating a class to hold a Students information, and a method that creates an instance and fills it in. If there is a problem, give the user a change to re=enter that user faulty data instead of a generic "fail and ignore". Store the instances in a collection of the class. Each element of the class should be appropriate to the data it contains, rather than just string: Age is a number (or better store the date of birth as a Date instead as Age is a "moving feast". ID is probably a number, but will have a "valid format" even if it is a string (and shouldn't be duplicated), and so forth. So validate and convert the user input to that type!

Then work out what "show index with oldest age" means. Almost certainly, you want the Student with the largest value Age - so if you have already validated his input it's a number (or better a date) so that becomes a pretty trivial loop.

Don't just rush into code like you have here: five minutes of thinking and planning can save you a lot of work later!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900