2

I have a text file that reads

NFR1: (some text) 
NFR2: (some text)
NFR3: (some text)

I wanted to group all strings in a text file that had the pattern"N".

This is code I have so far:

import java.util.List;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

public static void main(String[] args) throws FileNotFoundException {
    List<String> result = new ArrayList<String>();
    Pattern patt = Pattern.compile("^N.*");
    BufferedReader r = new BufferedReader(new FileReader("fileName"));

    String line;
    try {
        while ((line = r.readLine()) != null) {
          Matcher m = patt.matcher(line);
          while (m.find()) {

            int start = m.start(0);
            int end = m.end(0);
            result.add(m.group());
            System.out.println(result);
          }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

The output is as follows:

[NFR1:]
[NFR1:,NFR2:]
[NFR1:,NFR2:,NFR3]

How do I get the array to read just

[NFR1:,NFR2:,NFR3]

without the other two elements above?

When I add the line of code to get the last element:

if (result != null && !result.isEmpty()) {
              System.out.println("Last element is:");
              System.out.println(result.get(result.size()-1));
             }

The output is:

The last element is:
NFR1:
The last element is:
NFR2:
The last element is:
NFR3:

What am I doing wrong?

1
  • Use System.out.println(result); at the end of try-catch block? You are printing the arraylist in every iteration of the loop. Commented Nov 1, 2018 at 3:22

2 Answers 2

1

You are not doing any logic wrong. Just that you are printing inside the while loop rather than outside.

while ((line = r.readLine()) != null) {
  Matcher m = patt.matcher(line);
  while (m.find()) {

    int start = m.start(0);
    int end = m.end(0);
    result.add(m.group());
    //System.out.println(result); printing inside and so first u add 1 then 2 then 3.
  }
}

System.out.println(result);//Move here
if(!result.isEmpty()){//result != null is redundant. You have already initialized.
  System.out.println("Last element is:" + result.get(result.size() - 1));
}
Sign up to request clarification or add additional context in comments.

Comments

1

Because you print the output in loop, each loop, it will have a output.

To correct it just move the output out of loop.

For example

List<Integer> rest = new ArrayList<Integer>();
for(int i=0;i<2;i++){
rest.add(i);
System.out.println(result);
}

output

i=0: [0]
i=1: [0,1]

to correct it:

List<Integer> rest = new ArrayList<Integer>();
for(int i=0;i<2;i++){
rest.add(i);
}
System.out.println(result);

The result will be output [0,1]

1 Comment

Thank you. I can't believe I didn't see that!

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.