2

I got problems with comparing two strings in my loop.

Say I want to put two words in an ArrayList. I decide write both words through inputDialogeBox and put them in the list. If the two words are the same the second word will not be added to the list.

But when I compare the two strings the program doesnt seem to care if they are identical and both are ending up in the list.

Heres my code:

package testing;

import javax.swing.*;

public class Testing {

    public static void main(String[] args) {
        Word c = new Word();

        // word 1
        String word;
        word = JOptionPane.showInputDialog("Write a word: ");
        System.out.println("word1 = " + word);
        c.setWord(word);

        // word 2
        word = JOptionPane.showInputDialog("Write a new word: ");
        System.out.println("word2 = " + word);
        c.setWord(word); // sätt kortet≤

        System.out.println("words = " + c.getWord().size() + " " + c.getWord());
    }
}

And my class:

package testing;

import java.util.ArrayList;

public class Word {

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

    public void setWord(String word) {
        // set first value in arraylist to start the loop
        if (words.isEmpty()) {
            words.add("default");
        }

        for (int i = 0; i < words.size(); i++) {
            if (!words.get(i).equals(word)) {
                words.add(word);
                System.out.println("words.get(i): " + words.get(i) + "  word: " + word);
                break;
            }
        }
    }

    public ArrayList getWord() {
        return words;
    }
}

My guess is that the problem is around I add one defaultvalue, just to get something to loop with. Otherwise if the ArrayList is empty I cannot start my loop. But maybe there is a better way?

Thanks

4
  • 2
    You are adding the word to the list if there are any words in the list that is not the word. Commented Sep 20, 2016 at 16:44
  • Sorry. Im a beginner :-) <br> 1) If the ArrayList is empty - add a first value to the list 2) I loop through that only value. 3) If the String (default) is not the same as the first word it adds the first word to the ArrayList 4) If the String (default and word1) is not the as the second word it adds it to the ArrayList But I guess Im messing it up :-) Commented Sep 20, 2016 at 16:44
  • 2
    If you want to prevent duplicates, use a Set, not a List. Commented Sep 20, 2016 at 16:46
  • (how do I edit my posts by the way with correct formating?)<br/> Sorry. Im a beginner :-) 1) If the ArrayList is empty - add a first value to the list 2) I loop through that only value. 3) If the String (default) is not the same as the first word it adds the first word to the ArrayList 4) If the String (default and word1) is not the as the second word it adds it to the ArrayList But I guess Im messing it up :-) Commented Sep 20, 2016 at 16:52

2 Answers 2

1

In these type of cases you should use Set instead of ArrayList. Still below code modification should help you.

public void setWord(String word) {'
        boolean found = false;    
        for (int i = 0; i < words.size(); i++) {
            if (words.get(i).equals(word)) {
                found=true;
                break;
            }
        }
        if(!found){
           words.add(word);
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

The issue is that you are not checking for all of the values, only one by one. For the first word, you check against 'default' and add to the list. For the second word, you check against 'default' first, and since this is not equal, you add that word to the list also.

public void setWord(String word) {

    boolean exists = false;
    for (int i = 0; i < words.size(); i++) {
        if (words.get(i).equals(word)) {
           exists = true;
           break;
        }
    }
    if (!exists) {
        words.add(word);
        System.out.println("words.get(i): " + words.get(i) + "  word: " + word);
    }
}

or you could use ArrayList contains:

public void setWord(String word) {

    if(!words.contains(word)) {
        words.add(word);
        System.out.println("words.get(i): " + words.get(i) + "  word: " + word);
    }
}

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.