Java Quiz

I have a quiz, everything works fine on the first

question, when I continue on to the secone question (i

have a continue button that resets all necessary

values) the application says the users answer is Wrong

even though it isn't. I even put message box's on the

Validate Answer function and it shows me that the

users input and the actual answer are exactly the same

but for some reason when the strings are compared

FALSE is returned. The strange thing about it is it

works grand the first time.

Here's the Validate ander fucntion:

//compare users answer with chordName

public void verifyAnswer(String s){

String output = "";

//******test messages

JOptionPane.showMessageDialog(null, "s "+s);

JOptionPane.showMessageDialog(null,

"chordName "+chordName);

boolean answerVerify = chordName.equals(s);

String bool = String.valueOf(answerVerify);

JOptionPane.showMessageDialog(null, "boolean

"+bool);

///////////////////////////////////////////////

if(chordName.equals(s))

{

score++;

output ="Correct!";

String str = String.valueOf(score);

resultText.setText(output);

scoreText.setText(str);

}

else if(!chordName.equals(s)){

output = "Hard Luck.";

resultText.setText(output);

String str = String.valueOf(score);

scoreText.setText(str);

}

}

I appreciate the help if you can give any.

David Lundy

[1588 byte] By [dave_lundy] at [2007-9-26 21:00:43]
# 1

What if there is an extra space or the differ in upper and lower case letters? You could also print out the string length of s and chordName (I assume chordName is a String).

Or dump the characters as ints for each of them:

for (int x = 0; x < s.length(); x++) {

System.out.print((int)s.charAt(x)+" ");

}

System.out.println();

for (int x = 0; x < chordName.length(); x++) {

System.out.print((int)chordName.charAt(x)+" ");

}

System.out.println();

Then you can really see if there is any difference between them.

abnormal at 2007-7-3 20:11:12 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2
you're abnormally good at this, you were right, they weren't exactly the same , it's still strange though, how are the 2 strings the same the first time round? ah well, i can fix that I'm sure.ThanksDave
dave_lundy at 2007-7-3 20:11:12 > top of Java-index,Archived Forums,New To Java Technology Archive...