Reputation: 1
I have an assignment I have to complete for school to make a GPA calculator. It has three classes all together. In the class I am working in, we had to set up multiple methods. I this particular method, the "readGradeCount" method, the user is supposed to enter how many grades they want to calculate for their GPA. The user is supposed to enter a positive number and one that is greater than zero. I have to validate this users input, so I decided to do this by using a loop. When I run the code it gives me the following
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]
I have no idea what this means, and I have tried many things to fix it but I can't. This is my code for this method:
public static int readGradeCount()
{
int count;
Scanner keyboard;
keyboard = new Scanner(System.in);
do
{
System.out.print("How many grades? ");
while(!keyboard.hasNextInt())
{
System.out.print("Bad value- " + keyboard + "\n");
keyboard.next();
System.out.print("How many grades? ");
}
count = keyboard.nextInt();
}
while (count <= 0);
return count;
}
The value the user enters is supposed to come after the phrase "Bad value- " but I am getting that line of stuff instead. Any suggestions? Thanks for the help!
Upvotes: 0
Views: 96
Reputation: 146
You are printing the scanner and that gives a very weird string. Try to capture the value of the keyboard first and printing that.
while (!keyboard.hasNextInt()) {
final String value = keyboard.next();
System.out.print("Bad value- " + value + "\n");
...
Upvotes: 1
Reputation: 860
You are printing out the Scanner keyboard
in the println
statement, try using keyboard.next()
in the println
and then deleting keyboard.next()
in your program.
Upvotes: 2