0

I get the following error while compiling:

filimon.java:12: error: cannot find symbol
                }catch(InputMismatchException ime){
                       ^
  symbol:   class InputMismatchException
  location: class filimon
1 error

My source code is:

class filimon{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        try{
            System.out.println("enter 2 integer values");
            int a=s.nextInt();
            int b=s.nextInt();
            System.out.println("value of a: "+a);
            System.out.println("value of b: "+b);
        }catch(InputMismatchException ime){
            System.err.println("please enter only number value");
        }
        catch(Exception e){
            System.err.println(e);
        }
    }//main
}//filimon

What is the problem? Please help me.

Gherbi Hicham
  • 2,416
  • 4
  • 26
  • 41
  • What are your imports? – Daniel M. Jul 26 '16 at 20:48
  • Undoubtedly you have not identified to the compiler what package holds the `InputMismatchException` type. Also, you don't handle the exception, and `catch(Exception e)` is an antipattern. Please follow the naming conventions. – Lew Bloch Jul 26 '16 at 20:53

2 Answers2

2

Add import java.util.InputMismatchException;

PKR
  • 228
  • 2
  • 16
0

Java can't find InputMismatchException because it isn't imported.

InputMismatchException is located in java.util.

At the top of your file, write

import java.util.InputMismatchException;

to import just the exception, or

import java.util.*;

to import everything in java.util.

As a sidenote, the

catch(Exception e)

is not a good idea. It is better to list each exception that you would like to catch, either in its own catch block, or like this:

catch(InputMismatchException|NoSuchElementException e){
Daniel M.
  • 1,433
  • 13
  • 24