How to detect blank/empty line in input?

Write here if you have problems with your Java source code

Moderator: Board moderators

Post Reply
evandrix
New poster
Posts: 8
Joined: Wed Oct 03, 2007 11:07 am

How to detect blank/empty line in input?

Post by evandrix »

Hi to all reading this,

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

calling it:

Code: Select all

while ((input = Main.ReadLn(300000)) != null) {
...read data
}

but this returns the following error if i input a blank line in stdin (i want my program to exit on encountering a blank line, but i'm not sure what condition to check for - =null, .length()==0 both don't work)

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)

2. Another way is to use the following code:

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();}
}

...then calling it from my function...

Code: Select all

final MyBufferedReader console = new MyBufferedReader(System.in);

try{temp=console.readLine();}
catch (IOException ioex) {}

Not sure how i should test for whether the input is a blank/empty line..i have tried

Code: Select all

idata = new StringTokenizer(temp);
if (!idata.hasMoreTokens()) return;

to test it or even using

Code: Select all

while (idata.hasMoreTokens()){
...read input
}

...but this does not work very well all the time.

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

Anyone can suggest the most elegant way of reading IO and detecting blank/empty lines in input for JAVA?

Any assistance is much appreciated.
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

Why not use standard java.io.BufferedReader?

Code: Select all

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
    String line = reader.readLine();
    if (line == null) {
        // end of file
        break;
    } else if (line.length() == 0) {
        // a blank line
    } else {
        // not a blank line
    }
}
Post Reply

Return to “Java”