javainputmismatchexception

Getting exception in thread "main" java.util.InputMismatchException


I am trying to take input one by one and then trying to perform operations over them. Following my code.

   import java.util.Scanner;

    public class PlayerRoster {
       public static void main(String[] args) {
          Scanner scnr = new Scanner(System.in);
      
          final int LENGTH = 5;
          final char quit = 'q';
          char uin = 'z';
          int jersey = 0;
          int chng = 0;
      
          int[] number = new int[LENGTH];
          int[] rating = new int[LENGTH];
      
          for(int i = 0; i < LENGTH; i++){
             System.out.println("Enter player "+(i+1)+"'s jersey number:");
             number[i] = scnr.nextInt();
             System.out.println("Enter player "+(i+1)+"'s rating:\n");
             rating[i] = scnr.nextInt();
          }
          for(int i = 0; i < LENGTH; i++){
             if (i == 0)
                System.out.println("ROSTER");
      
                 System.out.println("Player " + (i + 1) + " -- Jersey number: " + (number[i]) + ", Rating: " + (rating[i]));
             if (i == 4)
                System.out.println("");
      
          }
          while (uin != quit){
         
             System.out.println("MENU");
             System.out.println("u - Update player rating");
             System.out.println("a - Output players above a rating");
             System.out.println("r - Replace player");
             System.out.println("o - Output roster");
             System.out.println("q - Quit\n");
       
             System.out.println("Choose an option:");
              uin = scnr.next().charAt(0);
      
             if (uin == 'o')
                for(int i = 0; i < LENGTH; i++){
                   if (i == 0)
                      System.out.println("ROSTER");
                      System.out.println("Player " + (i + 1) + " -- Jersey number: " + (number[i]) + ", Rating: " + (rating[i]));
                   if (i == 4)
                      System.out.println("");
                }
         
         
             if (uin == 'u')
                System.out.println("Enter a jersey number: ");
                   chng = scnr.nextInt();
         
          }  
       }
    }

I am not currently focused on logic as I am not able to take the input, as I am getting Exception in thread "main" java.util.InputMismatchException When simply trying to get more user input.

My Inputs are: 20 5 30 2 50 4 60 8 93 9 u 20 q

I have tried everything from char to normal Strings. It feels as if the scanner just isn't working or something.

    Exception in thread "main" java.util.NoSuchElementException
        at java.base/java.util.Scanner.throwFor(Scanner.java:937)
        at java.base/java.util.Scanner.next(Scanner.java:1594)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
        at PlayerRoster.main(PlayerRoster.java:55)

Solution

  • The issue is the code block

     if (uin == 'u')
            System.out.println("Enter a jersey number: ");
               chng = scnr.nextInt();
    

    Since you want the jersey number only in case when user gives input as 'u' your chng = scnr.nextInt(); should be inside the if block. In your case even if user gives input as 'q', it is still expecting more input from the user, and probably you are just giving blank space in that case giving you InputMisMatchException.

    To fix it please update code to

    if (uin == 'u')
    {
         System.out.println("Enter a jersey number: ");
         chng = scnr.nextInt();
    }
    

    So now if you give input as 'q' your loop stops executing which is the expected output.