How do i detect a blank/empty line in input using JAVA?
Thus far, I have been using the following 2 ways to read input data to be processed in my solution.
1. using the sample code under "How to submit in JAVA" on programming-challenges.com..
make use of this function defined in Main:
Code: Select all
static String ReadLn (int maxLg) // utility function to read from stdin
Code: Select all
while ((input = Main.ReadLn(300000)) != null) {
...read data
}
Code: Select all
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
at Main.Begin(Main.java:48)
at Main.main(Main.java:32)
Code: Select all
class MyBufferedReader {
private InputStream in;
public MyBufferedReader (InputStream in) {this.in = in;}
public String readLine() throws IOException {
final StringBuffer sb = new StringBuffer(80);
int i = 0;
while (((i = in.read()) != '\n') && (i != -1))
if (i != '\r')
sb.append((char) i);
if (i == -1)
return null;
return sb.toString();}
}
Code: Select all
final MyBufferedReader console = new MyBufferedReader(System.in);
try{temp=console.readLine();}
catch (IOException ioex) {}
Code: Select all
idata = new StringTokenizer(temp);
if (!idata.hasMoreTokens()) return;
Code: Select all
while (idata.hasMoreTokens()){
...read input
}
I have experienced getting compiler error for using trim() function.(althought i find this the most reliable) ie.
Code: Select all
final MyBufferedReader console = new MyBufferedReader(System.in);
try{temp=console.readLine();}
catch (IOException ioex) {}
if (temp.trim().length() == 0) return; //blank or empty line detected
Any assistance is much appreciated.