17
import java.util.Scanner;
class MyClass
{
    public static void main(String args[])
    {
        Scanner scanner = new Scanner(System.in);
        int employeeId, supervisorId;
        String name;
        System.out.println("Enter employee ID:");
        employeeId = scanner.nextInt();
        System.out.println("Enter employee name:");
        name = scanner.next();
        System.out.println("Enter supervisor ID:");
        supervisorId = scanner.nextInt();
    }
}

I got this exception while trying to enter a first name and last name.

Enter employee ID:
101
Enter employee name:
firstname lastname
Enter supervisor ID:
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 com.controller.Menu.<init>(Menu.java:61)
    at com.tests.Employeetest.main(Employeetest.java:17)

but its working on if I only enter the first name.

Enter employee ID:
105
Enter employee name:
name
Enter supervisor ID:
501

What I want is to read the full string whether it is given as name or as firstname lastname. What's the problem here?

Michael
  • 41,989
  • 11
  • 82
  • 128
09Q71AO534
  • 4,300
  • 13
  • 44
  • 68
  • 4
    Change `scanner.next()` to `scanner.nextLine()`. – Matt Ball Jul 17 '13 at 04:33
  • `sid=(scanner.nextInt()); `it expects an int and not a string, this is why it works with the `501` – fmodos Jul 17 '13 at 04:33
  • Calling nextInt() was a problem as it didn't pick up the new line (when you hit enter). So, calling scanner.nextLine() after that does the work. See my answer and comment in code. – Sajal Dutta Jul 17 '13 at 04:45

4 Answers4

26
Scanner scanner = new Scanner(System.in);
int employeeId, supervisorId;
String name;
System.out.println("Enter employee ID:");
employeeId = scanner.nextInt();
scanner.nextLine(); //This is needed to pick up the new line
System.out.println("Enter employee name:");
name = scanner.nextLine();
System.out.println("Enter supervisor ID:");
supervisorId = scanner.nextInt();

Calling nextInt() was a problem as it didn't pick up the new line (when you hit enter). So, calling scanner.nextLine() after that does the work.

Michael
  • 41,989
  • 11
  • 82
  • 128
Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
1

What you can do is use delimeter as new line. Till you press enter key you will be able to read it as string.

Scanner sc = new Scanner(System.in);
sc.useDelimiter(System.getProperty("line.separator"));

Hope this helps.

blganesh101
  • 3,647
  • 1
  • 24
  • 44
1

Replace:

System.out.println("Enter EmployeeName:");
                 ename=(scanner.next());

with:

System.out.println("Enter EmployeeName:");
                 ename=(scanner.nextLine());

This is because next() grabs only the next token, and the space acts as a delimiter between the tokens. By this, I mean that the scanner reads the input: "firstname lastname" as two separate tokens. So in your example, ename would be set to firstname and the scanner is attempting to set the supervisorId to lastname

Marco Corona
  • 812
  • 6
  • 12
  • `scanner.nextLine(); //This is needed to pick up the new line ; System.out.println("Enter EmployeeName:"); ename=scanner.nextLine(); ` – 09Q71AO534 Jul 17 '13 at 05:08
0

You are entering a null value to nextInt, it will fail if you give a null value...

i have added a null check to the piece of code

Try this code:

import java.util.Scanner;
class MyClass
{
     public static void main(String args[]){

                Scanner scanner = new Scanner(System.in);
                int eid,sid;
                String ename;
                System.out.println("Enter Employeeid:");
                     eid=(scanner.nextInt());
                System.out.println("Enter EmployeeName:");
                     ename=(scanner.next());
                System.out.println("Enter SupervisiorId:");
                    if(scanner.nextLine()!=null&&scanner.nextLine()!=""){//null check
                     sid=scanner.nextInt();
                     }//null check
        }
}
Fr_nkenstien
  • 1,923
  • 7
  • 33
  • 66