Reputation: 1
The instructions read: Write a program that prompts the user to input a student first name followed by a space and an integer test grade, then the user will press enter and you will prompt them again for a name and a test grade; for a total of 10 students and 10 grades. (Ex: Dawn 100 ) ; You must check that the user enters a grade that is >= 0 and <= 100. If not, you must prompt the user with an error message and the user must input the grade again.
I can't figure out how to do this without getting a java.util.InputMismatchException error message.
Scanner input = new Scanner(System.in);
String[] students = new String[10];
int[] grades = new int[10];
for(int i = 0; i < students.length; )
{
System.out.println("Enter a student name and grade(between 0 and 100): ");
if(input.nextInt() > 0 && input.nextInt() <= 100)
{
students[i] = input.next();
grades[i] = input.nextInt();
i++;
}
else
{
System.out.println("*Error* Grade value must be between 0 and 100. Please Try again");
}
}
Upvotes: 0
Views: 1253
Reputation: 56423
I can't figure out how to do this without getting a java.util.InputMismatchException error message.
The first mistake you've made is reading user input incorrectly input.nextInt() > 0 && input.nextInt()
this is requiring the user to enter an integer value twice whereas what you want is name (string
) & grade (int
).
the solution below takes that mistake into consideration, however personally I would use a while loop, but since you're using a for loop already I have decided to incorporate with that.
full solution:
Scanner input = new Scanner(System.in);
String[] students = new String[10];
int[] grades = new int[10];
for(int i = 0; i < students.length; i++)
{
System.out.println("Enter a student name and grade(between 0 and 100): ");
String getInput = input.nextLine();
if(Integer.parseInt(getInput.split(" ")[1]) < 0 || Integer.parseInt(getInput.split(" ")[1]) > 100)
{
System.out.println("*Error* Grade value must be between 0 and 100. Please Try again");
i--;
}
else {
students[i] = getInput.split(" ")[0];
grades[i] = Integer.parseInt(getInput.split(" ")[1]);
}
}
note - the solution given only takes into consideration what you've mentioned within your question i.e it is expecting input as such Ousmane 20
, Brendon 23
, jack 30
etc, also you can make the program a bit more robust by adding more validations if needed maybe NumberFormatException
, Scanner#hasNextInt()
and so forth.
Upvotes: 0
Reputation: 86232
This is the main culprit:
if(input.nextInt() > 0 && input.nextInt() <= 100)
Your if
condition contains two (2) calls to input.nextInt()
, so at this point — before reading the name — your program will attempt to read two numbers from input and see if the first is greater than 0 and the second less than or equal to 100. And if it succeeded, it would not store the numbers read into any variables.
Instead you need to read the name into students[i]
first. Then read the grade into grades[i]
. Then check if the grade read is in the interval. I suggest you use a while
loop so that as long as the grade is outside the interval, you print the error message and read the grade anew. If the first grade read was OK, you while
loop won’t execute at all, so this is fine.
Upvotes: 0
Reputation: 347194
Let's take a closer look...
if(input.nextInt() > 0 && input.nextInt() <= 100)
You check the input for nextInt
, twice, so you're no longer actually comparing the same value, but also, you've asked for a String
and an int
value, but you've not checked for the String
first...
Assume I entered something like 500 -1
, then you're if
statement would pass successfully, because 500
is > 0
and -1
is <= 100
And if by some miracle, that worked, you're reading another String
and another int
from the stream...
students[i] = input.next();
grades[i] = input.nextInt();
So, for this to work, the input would have to be something like 1 2 John 50
... which would just be completely weird and completely void what you're trying to do.
Instead, ask for one piece of information at a time and process it, for example...
Scanner input = new Scanner(System.in);
System.out.print("User name: ");
String name = input.nextLine();
System.out.print("Grade: ");
String gradeValue = input.nextLine();
Scanner parser = new Scanner(gradeValue);
if (parser.hasNextInt()) {
int grade = parser.nextInt();
if (grade >= 0 && grade <= 100) {
// Good grade
} else {
System.out.println("!! " + grade + " is not a valid grade");
}
} else {
System.out.println("!! " + gradeValue + " is not a valid integer value");
}
Don't keep reading from the Scanner
when you're not expecting a value, extract the value you need and process it. Here I've used nextLine
to get the grade and a second Scanner
to parse it, it's safer and avoids the oddities where the new line is left in the buffer. It also gives you better control to process errors ;)
Upvotes: 1