How to test for blank line with Java Scanner?
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!");
How to detect an empty line in java scanner
You can take the input using scan.nextLine()
and then check whether user gave an input with a correct pattern IN A SINGLE LINE. You can prompts the user to give the input in a single line separated by a space:
System.out.println("Please enter two numbers in a single line separated by a space. (i. e.: 4 12)");
You can check the pattern and do your job through split and parsing as folows:
if (Pattern.matches("\\d+ \\d+", input)) {
String[] nums = input.split(" ");
int num1 = Integer.parseInt(nums[0]);
int num2 = Integer.parseInt(nums[1]);
....
}
The full program is as below:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
if (Pattern.matches("\\d+ \\d+", input)) {
String[] nums = input.split(" ");
int num1 = Integer.parseInt(nums[0]);
int num2 = Integer.parseInt(nums[1]);
if (num1 == num2) {
System.out.println("They are equal");
} else if (num1 < 0 || num2 < 0) {
System.out.println("No negative number!");
} else {
System.out.println("Their difference is " + Math.abs(num2 - num1));
}
} else {
if (input.isEmpty()) {
System.out.println("This is Empty");
} else {
System.out.println("Wrong! Please enter correct number");
}
}
}
UPDATE
If you want to make your program supportive to both single line input and double line input, you can refactor the code as follows:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter two numbers in a single line separated by a space. (i. e.: 4 12)");
String input = scan.nextLine().trim();
if (input.matches("\\d+ \\d+")) {
doParsingAndCalculation(input);
} else {
if (input.isEmpty()) {
System.out.println("This is Empty");
} else if (input.matches("\\d+")) {
String secondLine = scan.nextLine().trim();
StringBuilder stringBuilder = new StringBuilder(input);
if(secondLine.matches("\\d+")) {
stringBuilder.append(" ").append(secondLine);
doParsingAndCalculation(stringBuilder.toString());
} else {
System.out.println("Wrong! Please enter correct number");
}
}
else {
System.out.println("Wrong! Please enter correct number");
}
}
}
private static void doParsingAndCalculation(String input) {
String[] nums = input.split(" ");
int num1 = Integer.parseInt(nums[0]);
int num2 = Integer.parseInt(nums[1]);
if (num1 == num2) {
System.out.println("They are equal");
} else if (num1 < 0 || num2 < 0) {
System.out.println("No negative number!");
} else {
System.out.println("Their difference is " + Math.abs(num2 - num1));
}
}
Java scanner test for empty line
A loop should work, though you must actually call the isEmpty method and scan only once per iteration
String operation = "";
do {
operation = sc.nextLine();
} while(operation.isEmpty());
You could also use sc.hasNextLine()
to check if anything is there
Java Scanner stop reading after empty line
every time you call the nextLine()-Method the scanner effectively reads a line from the input! This means that you program jumps over every second line!
To avoid this assign the line read to a variable and then add the content of this variable to your list. For example like this:
List<String> list = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
String line;
while (!(line = scanner.nextLine()).equals("")) {
list.add(line);
}
list.forEach(s -> System.out.println(s));
Java scanner with integers, stop this loop with empty line
You could use in.nextLine()
instead of in.nextInt()
, and then use the isEmpty()
function to check if it is empty and Integer.parseInt()
to convert it back to an int. You can do it more efficiently but this should suffice
int num = 0;
String input = in.nextLine();
if(input.isEmpty()){
auth = false;
}
else{
num = Integer.parseInt(input);
System.out.println(num);
}
Java scanner to detect blank line?
Your input has as many lines as it has \n
characters. Given the input
"there\ncat\ndog\n"
the next-lines will be correctly divided as
"there\n"
"cat\n"
"dog\n"
(In other words, there is no fourth blank line, since it is not terminated by a \n
.)
Put differently, after the "dog\n"
has been read, the scanner (or buffered reader for that matter) has reached EOF and there's not even an empty line to return. (Note that when the lines are returned, the new-line character is stripped off.)
So, since this is the expected behavior, I don't know what the easiest fix is. I suspect that the best way to solve this is simply to append a \n
to the input, so that the loop runs an extra iteration.
How can i detect BlankLine that Scanner has receive?
You can implement custom function like below which will return you nextLine
if it is not empty.
public static String skipEmptyLines(Scanner fileIn) {
String line = "";
while (fileIn.hasNext()) {
if (!(line = fileIn.nextLine()).isEmpty()) {
return line;
}
}
return null;
}
Related Topics
Java Regex to Validate Full Name Allow Only Spaces and Letters
How to Create Unique Column With Jpa
How to Pass List in Postman in Get Request and Get in Getmapping
Redirecting to a Servlet from a Jsp on Button Click
Consider Defining a Bean of Type in Your Configuration
How to Call a Function from Other Activity Android
How to Use Variables Declared in Another Class
Rounding to the Nearest Hundered-Thousandths
How Pass Json Array in Angular Post Method and How Get This Array in API
Cannot Construct Instance of 'Java.Time.Localdate' - Spring Boot, Elasticseach, Jackson
How to Get the Ip of the Wifi Hotspot in Android
How to Mock a Rest Template Exchange
How to Check Whether a Given String Is Valid Json in Java
Get Unique Values from Arraylist in Java
Query in Repository With Limit
Splitting Data Inside Quotes and Comma Using Regex
The Package Org.W3C.Dom Is Accessible from More Than One Module: <Unnamed>, Java.Xml