New To Java - Help for Arrays

Hi everybody,

I got a problem and i will be happy if someone helps me with simple codes because i am a beginner.

I think my problem is easy to solve but i am not able to solve it by myself.

What i want is;

for example let

student[1]=1grademax[1] =60 grademin[1]=50

student[2]=2grademax[2] =70 grademin[2]=55

i want to use arrays and i want to enter the input like;

16050// (with seperates)(than press enter to go next line) ([1's])

27055//( [2's])

i wrote the code

for (i=0;i<=2;i++)

{

student<i>=input.nextInt();

grademax<i> =input.nextInt();

grademin<i>=input.nextInt();

}

than make some comparisons and print a result. for example ;

maxgrade = grademax[0] ;

mingrade = grademin[0] ;

for(sayac=1 ; sayac <= 2 ; sayac++)

{

if( grademax[sayac] > maxgrade )

maxgrade = grademax[sayac];

if( grademin[sayac] < mingrade )

mingrade = grademin[sayac];

}

But it only gives correct result if i enter the values in same line just like;1605027055

what do i need to add to loop to make it work when i enter student informations with different lines ?

[1668 byte] By [BG18a] at [2007-11-26 23:15:36]
# 1
no comment ?
BG18a at 2007-7-10 14:15:25 > top of Java-index,Java Essentials,New To Java...
# 2
Look for the scanner nextLine() method, it should help. Otherwise, the Scanner will be interpreting the newline character as an int, and that's very unlikely to have good results.
kevjavaa at 2007-7-10 14:15:25 > top of Java-index,Java Essentials,New To Java...
# 3
i couldn't find how to apply this method to my code.
BG18a at 2007-7-10 14:15:25 > top of Java-index,Java Essentials,New To Java...
# 4

First, don't store data in parallel arrays like that. Create a class that encapsulates all the corresponding, and store instances of that class in a java.util.List or array (or a java.util.Set if you don't really need an ordering).

I suppose you could use Scanner.nextLine after you get the third int on a line. That should get you past the newline and ready for more input.

You should probably always call hasNextInt before you try to read an int, so you can better anticipate bad input.

paulcwa at 2007-7-10 14:15:25 > top of Java-index,Java Essentials,New To Java...