Results 1 to 4 of 4
Thread: Loop problems
- 10-18-2010, 12:20 AM #1
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
Loop problems
I am attempting to write the following program, but running into problems and would like some advice.
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.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 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
- 10-18-2010, 12:24 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
You ask for input first, then output "Enter student name", same for the score. Just reverse the order.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++; }Ever seen a dog chase its tail? Now that's an infinite loop.
- 10-18-2010, 12:28 AM #3
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
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.
- 10-18-2010, 12:49 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Similar Threads
-
Problems with a loop calling data from an ArrayList.
By moriarty in forum New To JavaReplies: 30Last Post: 03-28-2010, 02:00 AM -
Infinite running loop problems
By BigDummy in forum New To JavaReplies: 5Last Post: 10-14-2009, 06:39 AM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM -
Newbie having problems with for loop
By Dannii in forum New To JavaReplies: 4Last Post: 04-13-2009, 11:52 PM -
Problems with while loop
By Albert in forum New To JavaReplies: 2Last Post: 07-04-2007, 07:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks