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)