My program should check if s is an empty string and if found so, it should print "Empty string" and prompt for new input. But my every first run without asking for s, prints "Empty String", afterward it runs perfectly!
Scanner input = new Scanner(System.in);
int t = input.nextInt();
while (t > 0) {
String s;
s = input.nextLine();
if (s.isEmpty()) {
System.out.println("Empty string");
s = input.nextLine();
}
}
How can I avoid the first "Empty String"?
PS- I tried -
s = input.next();
This solves the problem but now it won't let me input an Empty String into the program!
PPS- Check this out:
import java.util.*;
import java.lang.*;
import java.io.*;
class ComparePlayers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
while (t > 0) {
String s;
s = input.nextLine();
if (s.isEmpty()) {
System.out.println("Empty String");
s = input.nextLine();
}
else {
System.out.println("Not Empty");
}
t--;
}
}
}
You can see there are 3 i/ps while one of the o/ps is taken up by the Empty String.