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?

1

3 Answers 3

2

Instead, you can do

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

and close the reader once done reading all the lines.

Sign up to request clarification or add additional context in comments.

Comments

1

Just loop until readLine is null.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.