1
public ArrayList<String> getWords()
{
    int size1 = lines.size();
    int size2 = 0;
    int counter3 = 0;
    ArrayList<Integer> checkthewords;
    for (int x = 0; x < size1; x++)
    {
        size2 = lines.get(x).substring(x).length();
        for (int y = 0; y < size2; y++)
        {
            if (Character.isLetter(charAt(((lines.get(x)).indexOf(x, z + x)))))
            {
                words.set(z, lines.get(x).substring(x,z + 1));
            }
            else
            {
                checkthewords.set(counter3, words);
                counter3++;
            }
            if (checkthewords.get(x).equals(checkthewords.get(counter3)))
            {

            }
        }
    }
    return words;
}

The method above is a called getWords(). I am trying to get a word from a file and store it in the arrayList checkthewords. I want to make sure that a word is not going to be stored in the arrayList checkthewords more than once.

I have the if statement:

            if (Character.isLetter(charAt(((lines.get(x)).indexOf(x, z + x)))))

But, don't know where to go from there.

  • 1
    Can you use a set instead of a list? – shmosel Feb 13 '17 at 04:31
  • 1
    What should `getWords()` return? A unique list of words? A `LinkedHashSet` is probably the way to go for that (assuming you need to preserve insertion order). – Elliott Frisch Feb 13 '17 at 04:33
  • Do you mean a normal array? – user7282043 Feb 13 '17 at 04:33
  • getWords() should return an array or arraylist with all the words of the file – user7282043 Feb 13 '17 at 04:34
  • Does the order matter? – Elliott Frisch Feb 13 '17 at 04:35
  • We haven't learned linkedhashset – user7282043 Feb 13 '17 at 04:35
  • Yes, eventually, I will have to find the order in which the arraylist occurs – user7282043 Feb 13 '17 at 04:36
  • Basically, my output should be: "a word (something that begins with an a): the line of which that word occured on" – user7282043 Feb 13 '17 at 04:46
  • 1
    If this is homework, I think using a `LinkedHashSet` is probably not the desired solution, and instead manual handling of duplicates is expected. @J. Doe you could make your code a lot clearer if you used meaningful names for your variables: `size1`, `size2`, `counter3`, `x`, `y` - these all convey zero information as to what their purpose is. – Matt Feb 13 '17 at 04:54
  • @matt, I'm sorry, I do understand this. This is a bad habit I've gotten into with the variable names.. – user7282043 Feb 13 '17 at 04:59
  • I would change them, but they are used in other parts of my code – user7282043 Feb 13 '17 at 05:00
  • Like you stated, using linkedhashset is not the way. I would what to complete this process. – user7282043 Feb 13 '17 at 05:00
  • It's a good habit to break, early :) Using the right words will help you to understand your own code because it becomes easier to see where you are trying to do something wrong. If you use an IDE (like NetBeans or Eclipse) investigate `refactoring` which is an easy way to rename things in your files that ensures that you update all the references in your project. – Matt Feb 13 '17 at 05:02
  • Thanks for the advice – user7282043 Feb 13 '17 at 05:09

2 Answers2

0

You should use Set in Java to store elements when duplicates are not allowed.

A collection that contains no duplicate elements.

If you want to retain insertion order as well then use LinkedHashSet which is Hash table and linked list implementation of the Set interface, with predictable iteration order.

Kindly refer to below tutorials to understand application of Set in java.
Tutorial 1
Tutorial 2
Tutorial 3

See Also-:
HashSet vs TreeSet vs LinkedHashSet
HashSet vs LinkedHashSet

Community
  • 1
  • 1
Ashish Kumar
  • 916
  • 2
  • 15
  • 32
0

I'm pretty sure your code won't run at the moment. You are doing some strange things in there and I don't really understand it.

Try to approach this one step at a time.

The first step is to get the word from the file. Make sure you can parse the line and extract the word you want.

Then you need to check if the word exists in your checkthewords list. If it doesn't exist, add it. You can use the contains method provided by List to see if the list contains something.

if(!checkthewords.contains(word)) {
    // it's not in the list yet, add it
    checkthewords.add(word);
}

Also when you create your checkthewords list, you don't initialise it (so it's null):

ArrayList<String> checkthewords;

should be:

ArrayList<String> checkthewords = new ArrayList<String>();

And you shouldn't use checkthewords.set() like that. set is used to replace an existing element, not to add a new element. You could easily be setting an element that doesn't exist yet and throw an ArrayIndexOutOfBoundsException. Use checkthewords.add(word) to add something to your list.

See the ArrayList documentation.

set(int index, E element)

Replaces the element at the specified position in this list with the specified element.

It seems like you're overthinking this. Keep it simple. :)

Community
  • 1
  • 1
Matt
  • 3,677
  • 1
  • 14
  • 24