How to test for blank line with Java Scanner?
问题 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.hasNext()) { // process input } But that will get me stuck in the loop 回答1: 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!"); 回答2: From http:/