For Loop not working correctly.
Hey
This is probably something easy. In fact, it definitely is. Still new to Java so ignore lack of arrays and better ways of doing it, as I'm only 172 pages into a 1300 page college book!!
Basically, what the code is to do is to get the user to enter a number of students. The user then enters a name and a score associated with this name (for the purpose of the code I am using 5 students - Chris 17, Tony 25, Zach 3, Michelle 20, God 99). This code should then output the highest and lowest student - in the case of my examples, God being the highest with 99 and Zach the lowest with 3.
However, the for loop is terminating once I enter the second student, which it should allow me to enter a total of 4 users (1 user entered before the for loop and 4, in the case of this example, during the for loop. It does work, in that it gives me the highest and lowest correctly, but only of the two entries.
Any help appreciated.
Code :
import java.util.Scanner;
public class lowHighStudent {
/**
* @param args
*/
public static void main (String args[])
{
Scanner input = new Scanner(System.in);
System.out.print("Enter number of students: ");
int noStudents = input.nextInt();
System.out.print("Enter Student Name: ");
String studentHigh = input.next();
System.out.print("Enter Student Score: ");
int scoreHigh = input.nextInt();
int scoreLow = 99999, temp = 0;
String studentLow = "";
for(int i = 0; i < noStudents - 1; i++);
{
System.out.print("Enter Student Name: ");
String student1 = input.next();
System.out.print("Enter Student Score: ");
int score1 = input.nextInt();
if (score1 > scoreHigh)
{
temp = scoreHigh;
studentLow = studentHigh;
scoreHigh = score1;
studentHigh = student1;
scoreLow = temp;
}
else if (score1 < scoreLow)
{
scoreLow = score1;
studentLow = student1;
}
}
System.out.print("Highest Student is " + studentHigh + " with a score of " + scoreHigh);
System.out.println("Lowest Student is " + studentLow + " with a score of " + scoreLow);
}
}
Re: For Loop not working correctly.
Well, with your < noStudents-1, you're skipping the last student.
Also, scoreLow is pretty high, being in the thousands.
Also, you might want to try input.nextLine() for the student name.
Unless you're going for a first name or last name only.
I tried "Peter Parker" and it threw an error.
Also, any reason why you're asking for the input before the for loop and then doing it again in the for loop?
If you're going that route, for loop from 1 to less than noStudents.
I'm a bit confused as to why you're entering a studentHigh and asking for a name.
Re: For Loop not working correctly.
True regarding the going for it before the for loop, now that I look for it. The scoreLow is set high as a basic error trap (there can't be a higher entry than 32k or what not as it's an int, and therefore any initial entry lower than the first score entered will always be lower than the high initial value [if I initialise scoreLow to 0 for example, no score entered can be lower than zero])
In terms of the name, nextLine would be better but it's not important in the spec of the example I have to complete but it's definitely something I would need to watch out for myself.
for noStudents - 1 though, my reasoning is that if I declare there are 5 students, and i = 0, then five students are 0, 1, 2, 3 and 4. If I say i < noStudents without the -1, that will give me 6 students.
Re: For Loop not working correctly.
Only if you started at index 0.
Also, if you got rid of that ; after the for loop, then it might work better.
for(int i = 0; i < noStudents - 1; i++);
^
Re: For Loop not working correctly.
Removing the ; fixed the for loop anyways, and it allowing me to enter 5 students (again in the example)
My if statements are a bit screwy though as it is giving me the highest student, but an incorrect lowest.
Re: For Loop not working correctly.
And fixed. Removed unnecessary temp variable for swapping high to low and just initialized scoreLow as whatever scoreHigh was and the else if sorted the rest. Thank you very much for the help. That ; on the for loop caught me in another example I was doing too!