How to test for blank line with Java Scanner?

后端 未结 5 1313
难免孤独
难免孤独 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:34

    Scanner key = new Scanner(new File("data.txt"));
    String data = "";
    
    while(key.hasNextLine()){
        String nextLine = key.nextLine();
    
        data += nextLine.equals("") ? "\n" :nextLine;
    
    }
    System.out.println(data);
    

提交回复
热议问题