Getting Exception " java.util.InputMismatchException" in scanner package
hey, thanks for helping me out before i've developed some knowledge of the scanner class now, though i have run into another problem, i have made an example program to show my error.
Code :
package testpackage;
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
System.out.println("input");
//for (int Loop = 0;Loop < 1;){
Scanner sc = new Scanner(System.in);
int name = sc.nextInt();
if(name > 0 && name < 41){
System.out.println("Working");
//Loop = 1;
}else{
System.out.println("error");
}
//System.out.println("Hello");
}
//}
}
my program works fine providing that the user will enter a numerical value, but if i add any value that is not numerical i get the error
Quote:
Exception in thread "main" java.util.InputMismatchException
i am unsure how to fix this error, i don't need to parse the character values though i will need it to continue working if the character value is entered.
thank you in advanced,
luke.
Re: checking if it is a character?
Hello luke,
The reason you are getting this error is because you are using int name = sc.nextInt();
For a start, the variable 'name' is an Integer so needs to have an Integer value assigned.
It's the Scanner class that is throwing the java.util.InputMismatchException exception.
sc.nextInt(); is looking for an Integer value entered into the console.
The Scanner class has several methods such as nextBoolean(), nextDouble(), nextFloat() which all look for the corresponding variable type.
If you want to use characters or String, then you can use sc.next()
Take a look at this example:
Code :
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
System.out.println("input");
Scanner sc = new Scanner(System.in);
String name = sc.next();
System.out.println(name);
}
}
We can catch this java.util.InputMismatchException exception in a try/catch block. This is error handling and will alert the user that they must use an Integer value.
Code :
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
try{
System.out.println("input");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
System.out.println(number);
}catch(Exception e){
System.out.println("You must enter a Integer value!!");
}
}
}
Re: checking if it is a character?
is there a way to loop this back so that it prompts the user again to enter the value?
Re: checking if it is a character?
Quote:
Originally Posted by
luke
is there a way to loop this back so that it prompts the user again to enter the value?
Yes there is. You can use a while loop:
Code :
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
System.out.println("Enter something!");
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String name = sc.next();
System.out.println(name);
}
}
}
Re: checking if it is a character?
hey i think i need to explain myself a bit more.
i want to store a user input that can be used to compare a user input to another int,Thus the format must be in INT(i think...) form.
and i wanted it so that, if you recieved that error message(because the value entered was not of numerical value) then you would be told that there was an error in your input and it would loop and prompt you for an input once again.
cheers,
luke.
Re: checking if it is a character?
Take a look at this luke.
If the user enters a non Integer value, the exception is caught and the ScannerStuff() method is run again.
The 2 Integers myInt & yourInt are compared once a user enters an Integer value. If they match then a message is printed to the console.
I hope this is what you are looking for...
Code :
import java.util.Scanner;
public class Main2 {
public static void ScannerStuff() {
int myInt = 10;
int yourInt;
System.out.println("Enter an Integer: ");
Scanner sc = new Scanner(System.in);
try {
while (sc.hasNext()) {
yourInt = sc.nextInt();
System.out.println(yourInt);
if(yourInt == myInt){
System.out.println("You entered the same Integer as me!");
}
}
} catch (Exception e) {
System.out.println("You must enter an Integer!");
ScannerStuff();
}
}
public static void main(String[] args) {
Main2 m = new Main2();
m.ScannerStuff();
}
}
Re: checking if it is a character?
This is an excellent an idea! thank you very much was exactly what i was looking for thank you!
Re: checking if it is a character?
alright last bit, extremely basic i know but i've forgotten.
i want it to check if the value entered is withing the boundaries.
for example i want in common language to be
if the entered value is greater than 0 and less than fourty then{
}
Re: checking if it is a character?
Quote:
Originally Posted by
luke
alright last bit, extremely basic i know but i've forgotten.
i want it to check if the value entered is withing the boundaries.
for example i want in common language to be
if the entered value is greater than 0 and less than fourty then{
}
This can easily be done with an if statement:
Code :
if(yourInt > 0 && yourInt <= 40){
System.out.println("Your value is greater than 0 and less or equal to 40");
}
This is basically saying, if yourInt is greater than 0 and less or equal to 40 then print message.
The > && <= bits are called Operators. Here is a quick overview:
Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
Re: checking if it is a character?
Re: checking if it is a character?
Quote:
Originally Posted by
luke
This is an excellent an idea! thank you very much was exactly what i was looking for thank you!
Brilliant! Glad I could help Luke.
If you have no further questions, please can you mark this thread as solved. (Check the link in my signature)
Thanks!!