How to read strings from a Scanner in a Java console application?

后端 未结 4 613
萌比男神i
萌比男神i 2020-12-01 18:46
import java.util.Scanner;
class MyClass
{
    public static void main(String args[])
    {
        Scanner scanner = new Scanner(System.in);
        int employeeId,          


        
4条回答
  •  广开言路
    2020-12-01 19:08

    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
            }
    }
    

提交回复
热议问题