0

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");
     }   
  }
1
  • also, using a while loop would be more suitable for the case that the user doesn't enter the required grade. though you could do it with a for loop with a little bit of altering. Also, this --> if(input.nextInt() > 0 && input.nextInt() <= 100) will require a user to enter twice which i think is not what you want there. Commented Apr 22, 2017 at 0:26

3 Answers 3

1

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 ;)

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

7 Comments

And if input Wyne Rogers 50 how would that cope ;)
@MadProgrammer good point, i will edit it soon, just trying to add some context. Thanks ;)
I think we can ignore the case of two names.
Modifying the for loop control variable (i) inside the loop makes for unreadable code. Please don’t teach the young ones (nor anyone else) such bad manners.
@OusmaneMahyDiaw We should also avoid copy/paste answers without at least providing context to why it would solve the problem and why it's a better solution and in what ways it improves upon what the OP is doing ... given that fact that's it's probably home work ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.