0

Hi I just wanted to know what is wrong with my code and how can I improve it as it throws an error "stream has already been operated upon or closed" thank you here is my code:

Stream<String> stream = Files.lines(path)

stream.filter(item -> !item.contains("String")).forEach(item -> {
            Pattern pattern = Pattern.compile(REGEX);
            Matcher matcher = pattern.matcher(item);
            if (!stream.filter(items -> items.length() > 12).collect(Collectors.toList()).isEmpty() == true) {
                log.error("Item must not exceed 12 numbers {}", item);
                streamMessage.set("Items must not exceed 12 numbers");
            }
            else if(matcher.matches() == true){
                log.error("Invalid String format {}", item);
                streamMessage.set("Invalid String format");
            }
        });

I wanted to loop inside the file and check per line if the characters there exceeded 12 characters or not

7
  • 1
    What exactly are you trying to achieve? Can you share a sample file and the result you'd like to get for it? Commented Jan 19, 2022 at 9:06
  • 1
    What do you want to achieve here? Besides the log messages, nothing comes out of your stream processing? Commented Jan 19, 2022 at 9:07
  • 1
    @CedrixCedrix even with the edit I'm not sure I understand. Do you mean you want to read the file and omit lines longer than 12 characters? Commented Jan 19, 2022 at 9:10
  • 1
    You're immediately closing the stream in the first iteration: stream.close(). On the next iteration, the stream access will throw the exception. Commented Jan 19, 2022 at 9:11
  • 1
    @CedrixCedrix stream.close() is unnecessary, but it's not the cause of the error. Commented Jan 19, 2022 at 9:16

1 Answer 1

1

You cannot reuse the Stream. You can create two Streams using Files.lines(path), but given your code, I don't think it's necessary.

You can find all the elements that are too long using a single Stream:

List<String> tooLong = 
    stream.filter(item -> !item.contains("String"))
          .filter(items -> items.length() > 12)
          .collect(Collectors.toList());

if (tooLong.size () > 0) {
    log.error("Items must not exceed 12 numbers {}", tooLong.toString ());
    streamMessage.set("Items must not exceed 12 numbers");
}
     

EDIT:

After your question edit, it appears you want to do two checks for each element of the Stream. You can still do it with a single Stream:

stream.filter(item -> !item.contains("String"))
      .forEach(item -> {
        Pattern pattern = Pattern.compile(REGEX);
        Matcher matcher = pattern.matcher(item);
        if (item.length() > 12) {
            log.error("Item must not exceed 12 numbers {}", item);
            streamMessage.set("Items must not exceed 12 numbers");
        } else if(matcher.matches()) {
            log.error("Invalid String format {}", item);
            streamMessage.set("Invalid String format");
        }
    });
Sign up to request clarification or add additional context in comments.

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.