How to test for blank line with Java Scanner?

后端 未结 5 1314
难免孤独
难免孤独 2020-12-02 23:58

I am expecting input with the scanner until there is nothing (i.e. when user enters a blank line). How do I achieve this?

I tried:

while (scanner.ha         


        
5条回答
  •  攒了一身酷
    2020-12-03 00:20

    Here's a way:

    Scanner keyboard = new Scanner(System.in);
    String line = null;
    while(!(line = keyboard.nextLine()).isEmpty()) {
      String[] values = line.split("\\s+");
      System.out.print("entered: " + Arrays.toString(values) + "\n");
    }
    System.out.print("Bye!");
    

提交回复
热议问题