(Java) nextLine() skipped in runtime, while working previously

前端 未结 1 1781
青春惊慌失措
青春惊慌失措 2021-01-22 11:30

In part of my java program I read a sequence of inputs for the user.

Scanner in = new Scanner(System.in);
System.out.println();
System.out.print(\"Enter student          


        
相关标签:
1条回答
  • 2021-01-22 11:52

    That's because after reading the int, the rest of the line (which is empty) is still unread and the next call to read nextline() will read the empty line.

    So when

    int studentNoParameter = in.nextInt(); 
    

    is executed, you enter the int and you hit enter, which creates a '\n' as input. The next call:

    in.nextLine(); 
    

    continues to read the rest of the line after the int, which is empty, and it encounters '\n' and terminates.

    0 讨论(0)
提交回复
热议问题