-1

I am trying to write a program with a method that will accept input and a method that will display that input. The main method will call those methods so the results display on the console. From what I have read if I use the nextLine() function to record a string input it will accept strings with spaces but even with that function if I run the program and input a string that has a space it gives me an error that basically says I typed something that could not be processed by nextLine() and I need to use a different function. Any idea whats going on? Also, I am new to this website so I apologize if the formatting is off.

import java.util.Scanner;
class Test {
   int employeeId;
   String employeeName;
   String designation;
   int salary;
     public void getTest() {
        Scanner ge = new Scanner(System.in);
            employeeId = ge.nextInt();
            employeeName = ge.nextLine();
            designation = ge.nextLine();
            salary = ge.nextInt();
        }  
public void showTest() {
    System.out.println(employeeId);
    System.out.println(employeeName);
    System.out.println(designation);
    System.out.println(salary);

    }
}
public class ScannerIssue {
    public static void main(String[] args) {
        Test test = new Test();
        test.getTest();
        test.showTest();
}

}

This is the error I get:

Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at Test.getTest(ScannerIssue.java:12)
at ScannerIssue.main(ScannerIssue.java:25)
  • 2
    If you look at the stack trace, it's barfing on `nextInt()`, not `nextLine()`. – azurefrog Mar 05 '20 at 18:56
  • I see that but this only happens when I input a string with a space. If I don't it works just fine. I assumed it had to do with nextLine() – George Seror IV Mar 05 '20 at 18:58
  • you will not be able to 'magically' convert a space into an `int` like that (so that's why it fails) – blurfus Mar 05 '20 at 18:59
  • employeeName and designation are strings and I have used the nextLine() functions with them. the other two are ints and I used nextInt() with them. I don't see the issue – George Seror IV Mar 05 '20 at 19:00
  • I suggest you use a debugger. BlueJ is a simple IDE and its debugger is easy to use. If you don't want to use a debugger, I suggest you put a print statement after each input from a Scanner. Something like: System.out.println("emplyeeId = " + emplyeeId); – NomadMaker Mar 05 '20 at 19:26
  • is this eventually [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045/85421) (asked at least once a week (seen 3 related questions in last two days) - very *popular*) – user85421 Mar 05 '20 at 19:29

3 Answers3

1

This fixes the issue

            Scanner ge = new Scanner(System.in);
            employeeId = Integer.parseInt(ge.nextLine());
            employeeName = ge.nextLine();
            designation = ge.nextLine();
            salary = Integer.parseInt(ge.nextLine());
            ge.close();

Scanner.nextLine() returns the line that was skipped, while Scanner.nextInt() returns the Int scanned from the input.

If you use scanner.nextLine() after scanner.nextInt(), then when you enter a number immediately followed by a linebreak, nextLine() is going to read an empty line, since no input exists in the line that was skipped after the number.

So when you enter

12345
John
Place
10000

Your original program scans 12345 to employeeId, and an empty String to employeedName. Then it scans "John" to designation. Last, when it tries to scan "Place" into salary, there is a mismatch exception.

MT756
  • 621
  • 1
  • 7
0

Just call the nextLine() method after the first call to nextInt() to clean input data.

     employeeId = ge.nextInt();
     ge.nextLine();
     employeeName = ge.nextLine();
     designation = ge.nextLine();
     salary = ge.nextInt();
Huatxu
  • 91
  • 5
0

An InputMismatchException occurs when you try to parse a String to an Object that isn't parsable. For example if you enter "Ok" for the employeeId it can't be parsed because it's not an Integer value.

It is also worth noting that scanners receive input after a new line is requested (when you hit enter for example) and each line must contain only the parsable data, not multiple unless you do that under the hood.

You should also be using next() instead of nextLine() for String in this case.

One more thing worth noting is that an individual method could be created for each request of data, and then a Test object be created from the result of that data. This would allow for requests of that data if an InputMismatchException occurs.

Jason
  • 4,590
  • 2
  • 9
  • 18