2 Replies - 226 Views - Last Post: 01 August 2013 - 06:38 AM Rate Topic: -----

#1 Killerspec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-April 13

Java BufferedReader issue

Posted 01 August 2013 - 06:28 AM

I found a work around to my issue but was just wondering if anyone could shed some light as to why when I am using bufferedReader and collecting information from a .txt file I land up missing the first line in the file. Is this something I have to work around all the time or is there something in my code that I am doing wrong. The code posted is the code from my class file and at present I am just printing it out to make sure that I am reading all the information in the file.
package the_prescription;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class ListOfMedicationAvailable 
{
     public void readFromFile()
    {
       File read= new File("medication.txt");
        FileReader fr = null;
        BufferedReader br = null;
        ArrayList<String[]> numbers = new ArrayList<String[]>();
        
        try
        {
             fr = new FileReader(read);
             //create a bufferreader object
             br = new BufferedReader(fr);
             
             String[] lineNum;
             
             String line = br.readLine();
             
             
             
             while(line != null)
             {
             //System.out.println(line);
                 lineNum = line.split(" ");
                 numbers.add(lineNum);
                 line = br.readLine();                       
                 System.out.println(line);
             }
             
        }
        catch(FileNotFoundException e)
        {
            System.err.println("File not here contact IT");
        }
        catch(IOException e)
        {
            System.err.println("File unavailable");
        }
        finally
        {
            try
            {
                fr.close();
                br.close();
            }
            catch(IOException e)
            {
                System.err.println("File not found");
            }
        }
    }
}


The text file I am reading from is 5 lines at the moment but I need to add more to it. At the moment I am just trying to get my structure right and will be building from there.

Contents of the text file is:

Paracetemol Glaxo 2.65 6
Norvasc Beecham 4.15 5
Augmentin Novartis 6.25 4
Zithromax Eli_Lilly 7.35 3
Tagamet Sanofi 4.15 3

At this point I simply added a line at the top of the information and that gives me all the information in the text file.

Thanks for the response in advance.

Matthew

Is This A Good Question/Topic? 0
  • +

Replies To: Java BufferedReader issue

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,645
  • Joined: 20-September 08

Re: Java BufferedReader issue

Posted 01 August 2013 - 06:34 AM

You're printing the line for the first time only after calling readLine for the second time. So you miss seeing the first line.

Make it easier on yourself - use this pattern

String line = null;
while((line = br.readLine()) != null) {
  // process
}

This post has been edited by g00se: 01 August 2013 - 06:35 AM
Reason for edit:: Clarification

Was This Post Helpful? 1
  • +
  • -

#3 Killerspec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-April 13

Re: Java BufferedReader issue

Posted 01 August 2013 - 06:38 AM

View Postg00se, on 01 August 2013 - 06:34 AM, said:

You're printing the line for the first time only after calling readLine for the second time. So you miss seeing the first line.

Make it easier on yourself - use this pattern

String line = null;
while((line = br.readLine()) != null) {
  // process
}


Thanks g00se I have been wrecking my head with it since lunch. Small things count and sometimes I am so involved that I am over looking the simple things.

Thanks a million hope you have a kick "*! day mate.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1