-1
String [] abcd={"A","B","C","D","E","F","G","H","I","J","K","L","N","M","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
JButton [] efgh = new JButton[abcd.length];
String [] secretWord={"S","W","I","N","G"};
JTextField [] input= new JTextField[secretWord.length];
String assign;
String [] content = new String[5];

for(int a=0;a<abcd.length;a++)
    {
        if(e.getSource()==efgh[a])
        {
            efgh[a].setEnabled(false);
            if(i<input.length)
            {
                input[i].setText(abcd[a]);
                i++;
            if(i==input.length)
            {   
                for(c=0;c<input.length;c++)
                    {
                        assign=input[c].getText();
                        content[c]=assign;
                        System.out.print(content[c]);

                    }
            }
        }
    }
        if(c==4)
        {
            if(content[0] == secretWord[0]) && content[1]==secretWord[1] && content[2]==secretWord[2] && content[3]==secretWord[3] && content[4]==secretWord[4])
            {
                output1.setText("You are right!!!!");
            }
            else
            {
                output1.setText("Try again!!");
            }
        }

    }   

when user click the button at all , i want to proceed the step content[value] with secretWord[value], but always got "Try Again".. where are the statement i have been wrong??please me thank you very much..

Cœur
  • 37,241
  • 25
  • 195
  • 267
noleavename
  • 83
  • 3
  • 15

2 Answers2

3

Welcome to StackOverflow!

Compare strings with .equals() not ==

if(content[0].equals(secretWord[0])) && .... 

-or-

if(content[0].equalsIgnoreCase(secretWord[0])) && ....
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
1

I agree with the the prior answer recommending using .equals in this situation. However, there is another technique for simplifying this code. If you want to do with arrays something that is easy with List objects, and supported for all List implementations, you can use Arrays.asList to get a List view of your arrays:

import java.util.Arrays;
public class Test {
  public static void main(String[] args) {
    String [] secretWord={"S","W","I","N","G"};
    String [] content = new String[5];
    System.out.println(Arrays.asList(secretWord).equals(Arrays.asList(content)));
    content[0] = "S";
    content[1] = "W";
    content[2] = "I";
    content[3] = "N";
    content[4] = "G";
    System.out.println(Arrays.asList(secretWord).equals(Arrays.asList(content)));    
  }
}
Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75