While coding a random Employee storer, I use Scanner, and its .nextInt(); to ask the user for the employee's contact number:

Scanner Scan = new Scanner(System.in);
out.print("\nEnter Employee Contact Number:");
newEmployee.contactNumber = Scan.nextInt();
//(I have a class Employee)

I get the error :

Enter Employee Contact Number:456645
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at employeestore.EmployeeStorer.addEmployee(EmployeeStorer.java:55)
	at employeestore.EmployeeStorer.main(EmployeeStorer.java:25)

I have done about half an hour of google-ing and I am currently *extremely* annoyed. Any help would be helpful. Thanks.

After a InputMismatchException the Scanner still contains the offending text, so you can catch the exception, then in the catch block use Scan.next() to see exactly what it was that it objected to.

I don't know how to use try and catch yet. Can you post that piece of code?

Did you enter an integer number? Usually when I got that exception with scanner it was because of that. Because I see this at what you posted:
Enter Employee Contact Number:456645

Are you sure that is where you get the error. The exception tells you where you got that error:

Enter Employee Contact Number:456645
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
[B]	at employeestore.EmployeeStorer.addEmployee(EmployeeStorer.java:55)[/B]
	at employeestore.EmployeeStorer.main(EmployeeStorer.java:25)

at employeestore.EmployeeStorer.addEmployee(EmployeeStorer.java:55)

Can you post some more code before and after the line: 25. Also indicate in your code that line.

try this syntax:

try
{
.....put ur code here..
}
catch(Exception ex)
{
System.out.println("Error is:" + ex)
}

try this syntax:

try
{
.....put ur code here..
}
catch(Exception ex)
{
System.out.println("Error is:" + ex)
}

No. not good advice. This suppresses the all-important stack info that tells you exactly where the exception was thrown. Use
ex.printStackTrace();
instead

Hmm. Strange. Tried the same code again just then and it seems to be working fine.. ??
Maybe it could have been my keyboard, but then what I posted a yesterday had the numbers right there, but with the problem after it. Now it reads it correctly, maybe it was a one off thing? (or something wrong with eclipse.?)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.