0

I have a problem with this code

String[] strLines = null;

while ((strLine = br.readLine()) != null){
    strLines = strLine.split("\\s");
    System.out.printf("next line - %s\n", strLine);
    for(String asd : strLines) {
        System.out.printf("array element - %s\n", asd);
    }
    System.out.println(strLines.length);
}

I'm trying to make a program read from file and then write all unique words into another file. The problem I'm having is that strLines array (which I later convert to Set) is overwritten with every iteration of while loop. Is it possible to somehow append to this array or should I use another way to store my words?

This might be a very beginner question (I've only been coding for a couple of months irregularly), but I couldn't find an answer to it anywhere. Thanks in advance!

1
  • strLines array (which I later convert to Set) Don't convert it later to a Set. Instead, create an empty set before the loop, and add every word you read to that set. The strLines array should be declared inside the loop. And it should be named words, since it's an array... of words. Commented May 2, 2017 at 18:58

4 Answers 4

5

There's no reason to create an array if all you do with it is converting it to a set later. Simply add to your set in the while loop:

String foo = "foo bar baz foo bar baz";
Set<String> fooSet = new HashSet<>();
fooSet.addAll(Arrays.asList(foo.split("\\s")));

For your example

Set<String> fooSet = new HashSet<>();
  
while ((strLine = br.readLine()) != null){
    fooSet.addAll(Arrays.asList(strLine.split("\\s")));
}
   

Or, in Java 9+, you can directly create an unmodifiable Set from the array by calling Set.of.

Set < String > set = Set.of( myArray ) ; 
Sign up to request clarification or add additional context in comments.

Comments

1

When you don't know what the exact size of your array might be, I'd use an ArrayList. An ArrayList does need an import given here: import java.util.ArrayList You also need to declare it in a certain way. For an ArrayList full of Strings is this: ArrayList<String> arrayListOfStrings = new ArrayList<String>(); For an ArrayList of type Object would be this: ArrayList<Object> arrayListOfObjects = new ArrayList<Object>(); You can make an ArrayList of any type of object. To add an item you use ArrayList's .add() function. IE: arrayListOfObjects.add(indexOfObject) ArrayLists also have .get(index), .remove(index) .add(index, Object) .size() etc. I hope that you find this brief tutorial on ArrayLists helpful!

Comments

0

You can use a List<String> collection to store all found words in this way:

List<String> words = new ArrayList<>();

    while ((strLine = br.readLine()) != null){
    String[] strLines = strLine.split("\\s");
    words.addAll(Arrays.asList(strLines)); 
    System.out.printf("next line - %s\n", strLine);
    }

    for(String word: words) {
        System.out.printf("word - %s\n", word);
    }
    System.out.println(words.size());

Comments

0

Thanks a lot for the answers! All of them have been really helpful, solved my issue with creating a set in the while loop, as baao suggested.

1 Comment

Please don't post such messages as answers. Answers should just be answers. Instead, upvote the answers you found useful, and optionally post a thank you comment on these answers.

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.