Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: For Loop not working correctly.

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

    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);
     
    	}
     
     
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.
    Last edited by javapenguin; May 15th, 2012 at 03:21 PM.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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++);
    ^
    Last edited by javapenguin; May 15th, 2012 at 03:32 PM.

  5. The Following User Says Thank You to javapenguin For This Useful Post:

    chrisob (May 15th, 2012)

  6. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

  7. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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!

Similar Threads

  1. Classpath override not working correctly in command prompt?
    By MeteoricDragon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 20th, 2011, 12:05 PM
  2. Hey guys, my While loop isn't working correctly.
    By metanoia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 26th, 2011, 11:15 PM
  3. While loop not iterating correctly
    By Rhyssa6 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 18th, 2011, 09:13 PM
  4. Replies: 3
    Last Post: November 9th, 2010, 01:19 PM
  5. Need help getting things to loop correctly
    By egruna2 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 9th, 2010, 04:53 PM