Lab Question

We must create a program that do the following:

Allow the user to type in a student name the coorosponds with the student's exam score.

The program will be able to accpet exam scores till the user types in the phrase "allDone".

The code will determine which student has the highest score and give the name and the score to the user.

Here is an example of what will be shown in the console....

Sample Input
Bob 82
Mary 90
James 87
alldone

Sample Output
Mary has the highest score. Her score is: 90.

Question

This line of code allows the user to type in the name of student and the grade that goes with the name:

//Allows for name and score to be type into system.
for(count = 0; count < numberOfStudents; count++)
{
   //Prompts user to type name of student.
    System.out.println("Type in the name of student " + count + ":");

    //Allows name of student to be stored in array.
    studentName [count] = input.nextLine();

    //Prompts user to type grade of student.
    System.out.println("Type in the score of student " + count + ": ");
    //Allows grade of student to be assigned in array.
    studentScore [count] = input.nextDouble();
}

I get an error message when I used this code. My question is this: In other words, how do I reformat this piece of code to allow the user to type in a name seporataly along with the corrospoindng exame score?

Recommended Answers

All 6 Replies

I get an error message when I used this code

Then why not post the error message and identify the line that throws it?

Sorry about that. This is the error message from the console:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at chapterSeven.LabOne_Q1.main(LabOne_Q1.java:57)

The line that it is refering to is this line: "studentScore [count] = input.nextDouble(); "

I suspect I know what the console is saying in the error message. It is because it is asking for a name and a score at the same time so when the user types in both, the type of information the program is accepting - a Double and a String - is does not match. I would need to let the user type in the name first, then the score seperately. How do I do this please?

Standard answer:

It's a real problem with Scanner's design - so many people fall into this trap.
You have some input alternating an int followed by some text, eg
101
John Doe
... and you try to read it with
int num = scanner.nextInt();
String name = scanner.nextLine();
... and name is an empty String ("")!

Even worse, the name is still in the input buffer so the next nextInt() call gets that and throws an exception.

Here's why:
Your input looks like this with the new line characters shown explicitly
101\nJohn Doe\n
nextInt takes the int value from the scanner, and stops when it finds a chaacter that's not part of an int, so it takes the "101", leaving this in the scanner
"\nJohn Doe\n"
then nextLine takes everything up to the first \n character - a zero-length String ("").

In this particular case the simplest solution is probably to call an extra nextLine() after the nextDouble() to clear the newline from the input stream

I want to make sure I understand what you are recommending. I think that you are saying to type a "println" in between the "nextLine()" and the "nextDouble()" however I tried that and it did not work. I get the same problem. Also, I may not be understanding the solution you are giving.

I think I will need to find a way to get the program to accpet the exam score first then ask the user for the person's name. Does anyone have any idea on how to do that? Thanks for the attempted help.

the simplest solution is probably to call an extra nextLine() after the nextDouble()

Please read what I wrote more carefully. I said to add a nextLine(), not a println, and I explained why.

Once again:
The user's input looks something like
"Brown\n123\nSmith\n456\nJones\n789\n" etc

Now remember that nextLine gets all the text up to the newline character and discards the newline char.
But nextDouble gets all the digits that are part of a valid double, then stops. It does not discard any newline char

So in your loop you execute:
nextLine() - returns "Brown" - leaves "123\nSmith\n456\nJones\n789\n"
nextDouble() - returns 123 -leaves "\nSmith\n456\nJones\n789\n"
nextLine - returns "" - leaves "Smith\n456\nJones\n789\n"
nextDouble() - sees "S" whic is not a double. Throws an exception.

so after each nextDouble call you need a call to nextLine() to clear out the \n that was left in the input buffer.

It doesn't mater whether you start the loop by asking for the name or the score. Once the loop gets going you alternate between name and score, with the problem and solution I explained above.

Thank You. I understand what you are saying now. Sorry that you had to explain again.

commented: Thats OK. Happy to help. JC +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.