[Q#7320315][A#7320393] How to test for blank line with Java Scanner?
https://stackoverflow.com/q/7320315
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: But that will get me stuck in the loop
Answer
https://stackoverflow.com/a/7320393
Here's a way:
APIzation
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!");
package com.stackoverflow.api;
import java.util.Arrays;
import java.util.Scanner;
public class Human7320393 {
public static void inputTillBlankLine() {
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!");
}
}
package com.stackoverflow.api;
import java.util.Arrays;
import java.util.Scanner;
/**
* How to test for blank line with Java Scanner?
*
* @author APIzator
* @see <a href="https://stackoverflow.com/a/7320393">https://stackoverflow.com/a/7320393</a>
*/
public class APIzator7320393 {
public static void test() throws Exception {
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!");
}
}