Click here to Skip to main content
15,886,065 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to compare values of two Jlabels i using below method but it always store false i mean always execute else portion how i can fix that problem ?
SQL
if ( lblslctdop1.getText( ) == lblcorrectans1.getText ( ) )
    {
    lblrightwrong1.setText ( "TRUE" );
    score++;
    }
else
    {
    lblrightwrong1.setText ( "FALSE" );
    }
Posted
Updated 1-Jun-15 11:44am
v3
Comments
[no name] 1-Jun-15 17:31pm    
Use the equals method to compare strings in java.
Member 11504333 1-Jun-15 17:48pm    
i am also using this but the same problem :(

if(lblslctdop7.equals(lblcorrectans7)){
lblrightwrong7.setText("TRUE");
score++;
}
else{
lblrightwrong7.setText("FALSE");
}
Sergey Alexandrovich Kryukov 1-Jun-15 17:39pm    
...but keep in mind that using the comparison from UI strings to fork semantically different action would be the abuse of technology.
Separate data model, application logic and UI.
—SA
Member 11504333 1-Jun-15 17:48pm    
i am also using this but the same problem :(

if(lblslctdop7.equals(lblcorrectans7)){
lblrightwrong7.setText("TRUE");
score++;
}
else{
lblrightwrong7.setText("FALSE");
}

1 solution

In Java you need to use .equals(..) to compare strings.

The code from your comment doesn't work because you're not checking the strings for equality but the labels.

This should work:
Java
if( lblslctdop1.getText().equals(lblcorrectans1.getText()) )
/* ... */
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Jun-15 18:57pm    
5ed.
—SA
Sascha Lefèvre 1-Jun-15 22:21pm    
Thank you, Sergey.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900