Search code examples
javaarrayscharalphabet

How do you compare two char arrays with each other to see if the inputted one has more than one specific variable than the other?


Okay, so I'm coding a program that will tell the user if the word they put into the command line is an isogram or not, and I'm having difficulties.

How exactly do I go about making it to where the user inputted char array is compared against the other to see if it has more than one letter than the other?

I have this and I'm confused where to go from here.

char[] alphabet = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char[] wordLetters = word.toCharArray(); 
boolean blnResult = Arrays.equals(alphabet,wordLetters);

I'm sure I went in the completely wrong way with this, but how should I go about comparing the two to get that result?


Solution

  • if I understand your problem right this is how I would do it:

    String isogram = "salad";
    boolean isIsogram = !isogram.matches( ".*(.).*\\1.*" );
    System.out.println( isIsogram );
    

    or inefficient but if you have to use arrays:

    String isogram = "salad";
    boolean isIsogram = true;
    for ( int i = 0; i < isogram.length(); i++ ) {
        for ( int j = 0; j < isogram.length(); j++ ) {
            if ( i != j && isogram.charAt( i ) == isogram.charAt( j ) && ( Character.isLetter( isogram.charAt( i ) ) ) ) {
                isIsogram = false;
            }
        }
    }
    System.out.println( isIsogram );