0

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.

Thihara
  • 7,031
  • 2
  • 29
  • 56
ZeW
  • 163
  • 2
  • 19
  • To make it a bit clearer ... if the user input `{'b', 'c','a', 'd', 'e', 'b'}` He would get `bac bad bec bed cab cad cab ceb ced ceb dab dac dab deb dec deb bac bad bec bed` the words cab deb etc. should not be in twice – ZeW May 29 '13 at 10:30

2 Answers2

1

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]))
Thihara
  • 7,031
  • 2
  • 29
  • 56
1

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>

nitegazer2003
  • 1,193
  • 5
  • 10
  • doesn't seem to go into that IF maybe it has something to do with the fact that when i print the array list it looks like this `[b, a, c, b, a, d, b, e]` – ZeW May 29 '13 at 10:35
  • After `//here it should check if the word already exists` in your code, remove away the whole block, then try my suggestion above. – nitegazer2003 May 29 '13 at 10:40
  • what I did and seems to work ` String word; word = array[i] + "" + array[j] + "" + array[k]; if (words.contains(word)){ continue; } else{ words += word + "\t"; }` basically what u suggested but i switched to a string instead of a char array list. One questions that bugs me though is ... isn't it bad to have a String declaration inside a loop ? doesn't that use too much memory ? – ZeW May 29 '13 at 10:43
  • It doesn't take up very much memory as your String is only 3 characters long. – nitegazer2003 May 29 '13 at 10:53
  • Would there be a more efficient way to do it ? If not, thank you for the help. – ZeW May 29 '13 at 10:59
  • In terms of memory efficiency, you will need to store every word that you need to check against at least once, so theres no way to use less memory than that. In terms of time efficiency, you can go faster by using a HashSet rather than an ArrayList to perform the `.add` and `.contains` operations. – nitegazer2003 May 29 '13 at 11:03