Search code examples
javajava.util.scannerbigintegerinputmismatchexception

Catch/try not working for input mismatch exception with scanner?


Alright so I'm taking a CS security class that has some basic java programing in it and our first assignment is to play with BigInteger. However, we also have to "bullet proof" our program. And while my method isn't the most ideal, it works EXCEPT for the first input. Aka if I type in an invalid input for any of the input.new---(); my program will prompt the user to try again with valid numbers. But the first input.nextInt(); will still take invalid input and crash, displaying the "java.util.InputMismatchException" and then rambles on about scanner. Any ideas as to why this is happening? p.s. The program must not display an error log under any circumstances.

import java.io.*;
import java.util.*;
import java.math.*;

class BigNumber{
   public static void main(String args[]){

     while(true){ 
      try{
         Scanner input = new Scanner(System.in);

         System.out.print("if you wish to exit the program type in '0,' to continue running the program type in any other value: ");
         double esc= input.nextDouble();
         if(esc == 0){ break;}
         else{System.out.println("Ok, program running...");}
         input.nextLine();

         System.out.print("Enter number a: ");
         String aValue = input.nextLine();

         System.out.print("Enter number b: ");
         String bValue = input.nextLine();

         System.out.print("Enter a number 'n': ");
         String nValue = input.nextLine();
         while (Integer.parseInt(nValue) < 1)
         {
            if (Integer.parseInt(nValue) < 1)
            {
               System.out.print("Please enter a valid number (n>1): ");
               nValue = input.nextLine();
            }
         }

         BigInteger a= new BigInteger(aValue);
         BigInteger b= new BigInteger(bValue);
         BigInteger n= new BigInteger(nValue);

         System.out.println("---------------------------");
         System.out.println("1) a xor b: " + a.xor(b));
         System.out.println("2) b xor b: " + b.xor(b));
         System.out.println("3) a xor b xor a: " + a.xor(b).xor(a));
         System.out.println(" ");
         System.out.println("4) ab mod n: " + a.modPow(b,n));
         System.out.println(" ");
         System.out.println("5) a shifted to the right by 6: " + a.shiftRight(6));
         System.out.println("6) b shifted to the left by 3: " + b.shiftLeft(3));
         System.out.println("---------------------------");

      }
      catch (NumberFormatException e){
         System.out.println("-----> Please try entering a valid number(s) <-----");
      }

    } 
  }
}

Solution

  • Oracle says

    public class InputMismatchException extends NoSuchElementException Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

    public class NumberFormatException extends IllegalArgumentException Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.

    you used only NumberFormatExceptionin the catch block so it only catch the NumberFormatException. To catch other exception you have to add other catch block

    catch (NumberFormatException e){
             System.out.println("-----> Please try entering a valid number(s) <-----");
    catch (InputMismatchException e){
             System.out.println("-----> Please try entering a valid number(s) <-----");
    

    OR if you used base Exception class it will catch any kind of Exception

    catch (Exception e){
             System.out.println("-----> Please try entering a valid number(s) <-----");