Question

the program i'm trying to make is thus: a program that makes three letter words in format from letters input by user. The same letter cannot be used more than once unless the user has used it more than once, and the same word cannot appear twice.

public class JavaApplication1 {

private static boolean Vowel (char c){
    return (c == 'a' || c == 'e' || c == 'o' || c == 'u' || c == 'i');
}

public static void main(String[] args) {

    char[] array = {'b', 'c','a', 'd', 'e', 'b'}; 
    //List<Character> chars = new ArrayList<Character>();
    String words = "";

    for(int i = 0; i < array.length; i++){ 
        if(Vowel(array[i]) == true){
            continue;
        }
        for(int j = 0; j < array.length; j++){
            if(Vowel(array[j]) == false){
            continue;
            }
            for(int k = 0; k < array.length; k++){
                if(Vowel(array[k]) == true){
                    continue;
                }
                if(array[k] == array[i]){
                    continue;
                }
                else{//here it should check if the word already exists 
                    if(chars.contains((array[i] + array[j] + array[k]))){
                        continue;
                    }
                    else{
                        chars.add(array[i]);
                        chars.add(array[j]);
                        chars.add(array[k]);
                    }
                }
            }
        }
    }
    System.out.print(chars.toString());
  }
}

The place I have trouble is ... checking if the word already exists. I've tried using Array lists strings, char array. (array[i]+array[j]+array[k]) seem to be perceived as a INT for some reason.

Was it helpful?

Solution

char data-type is basically a small int. They are not strings. Which is why you are getting integer arithmetic results instead of string concatenation.

If you want to know if your array list contains those three characters you need to call the contains method three times.

if(chars.contains(array[i]) && chars.contains(array[j]) && chars.contains(array[k]))

OTHER TIPS

You need to create a string from these and add it to the array.

String word = array[i] + "" + array[j] + "" + array[k];
if (chars.contains(word)) {
    continue;
} 
else {
    chars.add(word);
}

I assume chars is a List<String>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top