Geuni
Geuni

Reputation: 93

Looping through a file, a set of lines by lines

I have an issue with reading through a .dat file. Currently, I know how to read a .dat/.txt file, but this one is a little tricky for me.

So I have a set of data like this:

test.dat

Sunday
Scuba Diving Classes
Mr.Jones
N/A
Yes

Sunday
Sailing
Mr. Jackson
N/A
Yes

Sunday
Generation Next
Ms.Steele
N/A
Yes

Monday
Helping Hands
Ms.Wafa
ANX0
No

What I need to do with this file is to go through the file record by record, and if a string activityname .equals to the Name of the activity (e.g. Scuba Diving Classes), the variables starting with C are set for viewing. Here's the coding for it:

try{
            BufferedReader reader = new BufferedReader(new FileReader("test.dat"));

                int numberOfLines = readLines();
                numberOfLines = numberOfLines / 6;

                for(int i=0;i < numberOfLines; i++)
                {
                    String CDay = reader.readLine();
                    String CActivityName = reader.readLine();
                    String CSupervisor = reader.readLine();
                    String CLocation = reader.readLine();
                    String CPaid = reader.readLine();
                    String nothing = reader.readLine();

                    if(CActivityName.equals(activityName))
                    {
                        txtDay.setText(CDay);
                        txtSupervisor.setText(CSupervisor);
                        txtLocation.setText(CLocation);

                        //If it Activity is paid, then it is shown as paid via radiobutton
                        if(CPaid.equals("Yes"))
                                {
                                    radioYes.setSelected(rootPaneCheckingEnabled);
                                }
                        if(CPaid.equals("No"))
                                {
                                    radioNo.setSelected(rootPaneCheckingEnabled);
                                }
                    }
                    else
                    {
                        reader.close();
                    }
                }
        }
        catch(IOException e)
        {
                Logger.getLogger(AddActivity.class.getName()).log(Level.SEVERE, null, e);
        }
    }

I've used the numberoflines variable to go through the file, but my logic is flawed, and I have no idea how to go through this

Current status and error of method

Currently, this method only reads the first record Scuba Diving Classes, and does not go through the entire file when the if condition is false.

Help!

Upvotes: 1

Views: 1562

Answers (2)

tgkprog
tgkprog

Reputation: 4608

I see JB Net comment helped you. Also in general you should close a IO variable (file, data or other) in a finally block like so:

InputStream is = null;
try{
    //open streams, do work
}catch(...){

}finally{
//seperate try catch here to make sure it does not affect anything else, just close one resource per try catch
try{
    if(is != null){
        is.close()
    }catch(Exception ...){
        //one line log
    }
}

In java 7 you have try with resource but not tried that :)

Upvotes: 1

wchargin
wchargin

Reputation: 16057

Currently, this method … does not go through the entire file when the if condition is false.

To figure out why, take a look at the else block:

else
{
    reader.close();
}

This means that you are closing the BufferedReader before you have finished reading the file. You don't need to put anything in the else block there.

Upvotes: 2

Related Questions