0

I could not find an explanation and those I found I am unsure of. So please confirm my doubts:

I am reading through a file using a while loop and if the line in the file is empty it skips and goes to next line. I just want to make sure the code I am using is correct for the what I just described:

while((strLine = reader.readLine())!= null)  <----- While loop that is suppose to read Line by Line
{           
    if (strLine.isEmpty() == false) <----- Check for empty Line
    {
        /** My Code **/
    } 
    else 
    {
    /** My Code **/
    }
}   
awksp
  • 11,764
  • 4
  • 37
  • 44
user40380
  • 76
  • 8
  • 2
    Looks good, but there are these handy things called the Java compiler and the Java runtime that can do this stuff for you. – PlasmaPower May 11 '14 at 03:36
  • Sorry!! but What exactly you want to know? :) – akash May 11 '14 at 03:42
  • The reader us a buffer reader I can give the code if you want. And I am edit .java resource files then testing it by using Junit so I can't compile the file unless I create a new class and need to redo an entire program. – user40380 May 11 '14 at 03:43
  • You're asking us whether the code you've written does what you think it does? You're cloud-sourcing your JVM now? – Dawood ibn Kareem May 11 '14 at 04:06

4 Answers4

1

yes. it will work fine.

while(/* While scanner has next line */)
{
   line = scanner.nextLine();
   if( /* line is not equal to null */) {

    /* perform code */

    }
}
Dumindu Madushanka
  • 494
  • 1
  • 9
  • 19
1

Yes! What you are doing is what you want to do. You can just try compiling it yourself, you know. Trial and error. If you could not figure out how to use the reader, as the other answers propose, here you go:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Trial {

    public static void main(String[] args) throws IOException {
        String strLine;
        BufferedReader reader = new BufferedReader(new FileReader(
                "/home/user234/folder1/filename"));
        while ((strLine = reader.readLine()) != null) {
            if (!strLine.isEmpty()) {
                System.out.println("notEMPTY");
            } else {
                System.out.println("EMPTY");
            }
        }
    }
}
nook
  • 2,378
  • 5
  • 34
  • 54
natsirun
  • 28
  • 4
0

The Java Reader does not have a readline() method.

If you want to do specific parsing of tokens you should use the Scanner. Scanner has a nextLine() method to grab each line, but throws an Exception if there is no next line. Therefore you should use Scanner.hasNextLine() for your while condition.

Scanner s = new Scanner("filename.txt");
String line;

while(s.hasNextLine()){                            // check for next line
    line = s.nextLine();                           // get next line
    if(line == ""){                                // check if line is empty
        System.out.println("Empty");
    } else {
        System.out.println("Not Empty:" + line);
    }
}   

Here's a live Example using Ideone.


EDIT: The BufferedReader does have a readline() method, as used by @natsirun. Although for any file parsing more complicated than line reading you would prefer the Scanner.

Community
  • 1
  • 1
bcorso
  • 45,608
  • 10
  • 63
  • 75
0

The logic shown in the above code makes sense to what you have described. It should perform what you desire.

mikey13579
  • 11
  • 3