nmu
nmu

Reputation: 1519

Does Arrays.equals work with chars in java

I am trying to see if two char arrays are equal however for some reason they never seem to be correctly reading as equal to Java. i'm pretty sure this could be to do with my lack of understanding for the Arrays.equals() method so if anyone would care to explain what Arrays.equals() looks at in the array that would be great (from what i've seen from a simple google search it seems to check how many elements there are in the array and then the contents but i'm interestead in what the method looks at in the element for instance do capital letters make it inequal)

Since this is most probably just faulty code here is my code:

int count = 0;
    Highscore words = new Highscore();

    String word = words.getWord();
  word = word.toUpperCase();
    char guess;

    for(int i = 0; i<word.length(); i++)
    {
        charwo[count] = word.charAt(i);
        charda[count]= '_';
        count++;
    }

    int guesses =7;

    while(guesses != 0 || !Arrays.equals(charwo, charda))
    {
        System.out.println("");

        for(int l=0; l<count; l++)
        {
            System.out.print(" "+charda[l]);

        }


   guess = JOptionPane.showInputDialog("Enter letter ").charAt(0);


    if(word.toUpperCase().contains(String.valueOf(guess).toUpperCase()))
    {

        for(int k = 0; k<word.length(); k++)
        {
            if(String.valueOf(guess).toUpperCase().equals(String.valueOf(charwo[k]).toUpperCase()))
            {
                charda[k]=charwo[k];


            }

          }


    }


    else
    {

            guesses = guesses-1;

            System.out.println("guesses left "+guesses);
    }



            }

}

Upvotes: 0

Views: 419

Answers (4)

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35547

Arrays.equals() works on char arrays since that is also an array in java. I think you can check this by your own.

Upvotes: 0

Santosh
Santosh

Reputation: 17893

From the source code:

Arrays.equals checks following in order

  1. If they are same references return true.
  2. If either of them is null, return false
  3. If their length is not matching return false.
  4. Iterate over contents and compare each character at a index to char other array at same index. Return false at first mismatch.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691625

You probably want

while(guesses != 0 && !Arrays.equals(charwo, charda))

instead of

while(guesses != 0 || !Arrays.equals(charwo, charda))

Continue looping while the user still has guesses left AND he hasn't found the solution.

When in doubt about your algorithm, start the application in debug mode, execute it line by line, and evaluate the expressions to see what their value is. It's extremely easy with all the current IDEs (IntelliJ, Eclipse, NetBeans, for example).

Upvotes: 1

rocketboy
rocketboy

Reputation: 9741

Yes Arrays.equals() works on char arrays. char is a primitive type and can use == safely.

Also from documentation:

Returns true if the two specified arrays of chars are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.

Upvotes: 3

Related Questions