0

I have a problem. I created a application which reads a txt file and splits sentence using the substring function. This is code:

    public static void main(String[] args) throws IOException {

    String linia = "";

    Ob parser = new Ob();

        try{
           FileReader fr = new FileReader("C:/Dane.txt");
           BufferedReader bfr = new BufferedReader(fr);
           linia = bfr.readLine();

           parser.spl(linia);

        } catch( IOException ex ){
           System.out.println("Error with: "+ex);
        }

}

public class Ob {

   String a,b;

   public void spl(String linia)
   {
       a = linia.substring(14, 22);
       b = linia.substring(25, 34);

       System.out.println(a);
       System.out.println(b);
   }

}

That works properly, but I want to extend this application. I want to read every line of the text file and save the lines in a String array. I then want to pass every line from the array into my spl function. How can I do that?

Micah Hainline
  • 14,367
  • 9
  • 52
  • 85
edi233
  • 3,511
  • 13
  • 56
  • 97
  • Do you mean like this? http://stackoverflow.com/questions/5576404/i-want-to-read-a-text-file-split-it-and-store-the-results-in-an-array or http://stackoverflow.com/questions/5866707/performant-text-file-reading-and-parsing-split-like-needed etc? – Peter Lawrey Nov 11 '11 at 18:26

3 Answers3

2

Instead, you can do

while((linia = bfr.readLine()) != null) {
    parser.spl(linia);
}

and close the reader once done reading all the lines.

srkavin
  • 1,152
  • 7
  • 17
1

Just loop until readLine is null.

Micah Hainline
  • 14,367
  • 9
  • 52
  • 85
0

You can use something like that:

String linia = null;

while(linia = bfr.readLine() != null){
   String a = linia.substring(14, 22);
   String b = linia.substring(25, 34);

   System.out.println(a);
   System.out.println(b);
}
Phil
  • 3,375
  • 3
  • 30
  • 46