Results 1 to 4 of 4

Thread: Loop problems

  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default Loop problems

    I am attempting to write the following program, but running into problems and would like some advice.

    Java Code:
    /* TwoHighestScores.java
     * Jim 
     * 10/17/2010
     * This program prompts the user to enter the number oif students and each
       students name and score, and finally displays the student with the highest
       and the student with the second highest score */
    
    import java.util.Scanner;
    
    public class TwoHighestScores {
    	public static void main(String [] args) {
    		//Declare scanner variable for iput
    		Scanner input = new Scanner(System.in);
    		//Declare int variable for the number of students
    		final int NUMBER_OF_STUDENTS = 3;
    		//Declare string variable for student name
    		String studentName;
    		//Declare double variable for student score
    		double studentScore;
    		//Declare int variable for count
    		int count = 0;
    		System.out.println("This program finds the two highest scores" + "\n ");
    
    		//Create while loop to repeat 3 times
            while( count < NUMBER_OF_STUDENTS ) {
    			//allow input for student name
    			studentName = input.nextLine();
    			   //output to console
    			   System.out.println("Enter Student Name");
    			//allow input for student score
    			studentScore = input.nextDouble();
    			   //output to console
    			   System.out.println("Enter Student Score");
    			count++;
    
    		}
    
        }
    }
    I'm having two problems that I do not know how to resolve. The first is that when I run the program, it comes up to "This program finds the two highest scores" and then stops. It will not ask "Enter Student Name" until I hit enter.
    I want it to show the first sentence then drop to a new line and ask to enter the student name. I thought adding the \n would help but it didn't.

    The second problem is that after I enter a name and hit enter I get the following error:

    Java Code:
    Exception in thread "main" java.util.InputMismatchException
                at java.util.Scanner.throwFor(Scanner.java:840)
                at java.util.Scanner.next(Scanner.java:1461)
                at java.util.Scanner.nextDouble(Scanner.java:2387)
                at TwoHighScores.main(TwoHighestScores.java:31

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Java Code:
    while( count < NUMBER_OF_STUDENTS ) {
    			//allow input for student name
    			studentName = input.nextLine();
    			   //output to console
    			   System.out.println("Enter Student Name");
    			//allow input for student score
    			studentScore = input.nextDouble();
    			   //output to console
    			   System.out.println("Enter Student Score");
    			count++;
    
    		}
    You ask for input first, then output "Enter student name", same for the score. Just reverse the order.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    I've been banging my head against the monitor for an hour and couldn't see it until you pointed it out. arrrhgggh.

    Thank you very much.

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    No probs, it's always the little things that slip by and give us grey hairs.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Problems with a loop calling data from an ArrayList.
    By moriarty in forum New To Java
    Replies: 30
    Last Post: 03-28-2010, 04:00 AM
  2. Infinite running loop problems
    By BigDummy in forum New To Java
    Replies: 5
    Last Post: 10-14-2009, 07:39 AM
  3. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 09:46 PM
  4. Newbie having problems with for loop
    By Dannii in forum New To Java
    Replies: 4
    Last Post: 04-14-2009, 12:52 AM
  5. Problems with while loop
    By Albert in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 08:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •