Vittori
Vittori

Reputation: 111

How to skip the NULL line in java?

My program need to read .txt file and store it in to Arraylist.

But readLine() Stop reading after finishing A , B , and got Error ( when reading Blankline everything return NULL and got outbound exception)

.txt file

A B

C

D E

Does its possible to read , skipping blankline , read again , detect blankline and skip it again .......

    public static void loadData(){
    try{
        BufferedReader rd = new BufferedReader (new FileReader("/Users/Homura/Documents/newWorkspace/DataStructures/src/flights.txt"));

    while(true){

        String myLine = rd.readLine();
        String fName = myLine.substring(0,myLine.indexOf("->",0));
        String toName = myLine.substring(myLine.indexOf("->")+3);

        if(!myMap.containsKey(fName)){
           ArrayList<String> myArray = new ArrayList<String>();
           myMap.put(fName,myArray); 
        }
        myMap.get(fName).add(toName);
        allPlaces.add(fName);

        if(rd.readLine()== null) { myLine = rd.readLine();
       }
    }
    }

    catch(IOException ex){
        throw new ErrorException(ex);
    }
}

Upvotes: 1

Views: 4292

Answers (4)

LeleDumbo
LeleDumbo

Reputation: 9340

Yes, it's possible. But your Exception doesn't come from that. It's from null being applied indexOf() (see your exception stack trace, READ IT, that's a valuable source where you can track your errors). You use readLine() improperly. This:

if(rd.readLine()== null) { myLine = rd.readLine();
}

will throw away every odd line. You CAN'T check for null by calling readLine(). Instead you should call readLine() only once in the loop and check its RETURN VALUE for null. And only if it's not null you should do further processing.

Upvotes: 1

Vidya
Vidya

Reputation: 30320

Try with Scanner. It is a lot easier:

Scanner scanner = new Scanner(new File("/Users/Homura/Documents/newWorkspace/DataStructures/src/flights.txt");
while (scanner.hasNextLine()) {
  String line = scanner.nextLine();
  if (!line.equals("")) {
    //do stuff with the line since it isn't blank
}

This way you keep reading until the end of the file, and you only do stuff with lines that aren't blank.

You might also want to consider running the line through something like line.replaceAll("\\s+", ""); to ensure the blank lines are truly blank and don't have other whitespace characters.

Upvotes: 3

user207421
user207421

Reputation: 311054

There is no 'null line' in Java. If readLine() returns null it means end of stream, which you have to check every time you call it, and if you get it close the stream and stop reading. Your loop should look like this:

while ((line = in.readLine()) != null)
{
    // ...
}
in.close();

There is no such thing as 'outbound exception' either. Did you mean ArrayIndexOutOfBoundsException? or NullPointerException?

Upvotes: 2

codingenious
codingenious

Reputation: 8663

you are not getting exception because of anything being null. But because of this line

String fName = myLine.substring(0,myLine.indexOf("->",0));

as, myLine.indexOf("->",0) returns -1, so substring method throws the exception. Modify this code to get it working.

and don't use while(true), it will be in while loop forever. use

while(rd.readLine() != null)

instead.

Upvotes: 1

Related Questions