简体   繁体   中英

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

AB

C

DE

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

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.

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM