Home > OS >  Difference between two codes and InputMismatchException
Difference between two codes and InputMismatchException

Time:02-11

I am a newbie with java and the class scanners. I have two Codes and I dont get the point why one of them throws a InputMismatchException. I look forward to the answers. Here both codes:

1st one with the Exception Error:

public static void main(String[] args) {
    
    Scanner sc = new Scanner(System.in);
    
    String firstName, lastName, completeName;
    int age;
    
    System.out.println("Please enter your first name: ");
    
    firstName = sc.nextLine();
    
    System.out.println("Please enter your last name: ");
    
    lastName = sc.nextLine();
    
    System.out.println("Please enter your complete name and your age: ");
    
    completeName = sc.next();
    age = sc.nextInt();
    
    System.out.println("Your complete name is: "   completeName);
    System.out.println("Your age is: "   age);

}

Console:

Please enter your first name:

Peter

Please enter your last name:

Henrik

Please enter your complete name and your age:

Peter Henrik 22

(InputMismatchException)

2nd one with no error:

public static void main(String[] args) {
    
    Scanner sc = new Scanner(System.in);
    
    String firstName, lastName;
    int age;
    
    System.out.println("Please enter your first name: ");
    
    firstName = sc.nextLine();
    
    System.out.println("Please enter your last name: ");
    
    lastName = sc.nextLine();
    
    System.out.println("Please enter your age: ");
    
    age = sc.nextInt();
    
    System.out.println("Your complete name is: "   firstName   " "   lastName);
    System.out.println("Your age is: "   age);

}

Console:

Please enter your first name:

Peter

Please enter your last name:

Henrik

Please enter your age:

22

Your complete name is: Peter Henrik

Your age is: 22

CodePudding user response:

Your first code throws InputMismatchException because sc.next() reads the first complete token ("Peter") up to the whitespace (since whitespace is the default delimiter for Scanner in Java), thus after that, the sc.nextInt() method will read "Henrik" which is a String unlike your expectation to read the age (22).

Here's what you can do instead in order to read the complete name of the user as well as the age [CONDITION: You'll have to enter age in a new line]:

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String firstName, lastName,completeName;
int age;

System.out.println("Please enter your first name: ");

firstName = sc.nextLine();

System.out.println("Please enter your last name: ");

lastName = sc.nextLine();

System.out.println("Please enter your complete name: ");

completeName = sc.nextLine();

System.out.println("Please enter your age: ");

age = sc.nextInt();

System.out.println("Your complete name is: "   completeName);
System.out.println("Your age is: "   age);

}

PS: An alternate solution can be to read the complete name and the age in a single line as you did and then split up the tokens using the String split() method in Java.

CodePudding user response:

Having a look at the Javadoc of Scanner#next() will tell you the following:

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.

The default delimiter for the Scanner is a whitespace.

Knowing this, you provide the following input

Peter Henrik 22

and try to read this input via the following

completeName = sc.next();
age = sc.nextInt();

The call to sc.next() will read the first complete token up to the whitespace, which is "Peter". So Henrik 22 is still available in the Scanner buffer.

Therefore sc.nextInt() will read Henrik and try to parse it to an int hence the InputMismatchException.

To read both tokens to get the complete name, simply change

completeName = sc.next();

to

completeName = sc.next()   " "   sc.next();

However, names don't always consist of only two parts. Since there could be names that more than two single tokens, you should / could do it like the following (provided you still want to enter the complete name and age in one line):

String line = sc.nextLine(); // read the whole line
// everything up to the last token is the name
completeName = line.substring(0, line.lastIndexOf(" "));
// last token is the age
age = line.substring(line.lastIndexOf(" ")   1);
  • Related