0

I am still new to Java. I am trying to create a program where users must answer a multiple choice quiz. The users will input their answer and these inputs will be formed into an array. Then I plan on using a for loop to compare the user's answers array to the array of correct answers to tell the user if they are right or wrong.

However it seems that i am not properly comparing the 2 arrays in my if statement. Every time I run the program, it goes straight to the else statements.

My hunch is that the scanner class does not actually store the value?

Can anyone help?

Part of code below:

//Above this section is just a collection of "System.out.println" statements that state questions and answers the user choose from.




     int x;
                String answers [] = {"a", "a", "b"}; 
    //answers array has the correct answer 
                Scanner in = new Scanner(System.in);
                String answerEntered [] = new String [5]; 
    //user input will be in this arra

                for(x=0 ; x<3 ; x++)
                {
                    System.out.print((1+x)+". ");
                    answerEntered[x] = in.nextLine();
                }
                for( x=0; x<3; x++)
                {   

                    **if(answerEntered[x] == answers[x])
                    {
                        System.out.println("For Question "+(x+1)+", you are Correct!");
                    }**
     //This if section does not seem to work. Every time i run the code it automatically goes to the else statement. 

                    else
                    {
                        System.out.println("The correct answer for Question "+(x+1)+" is: "+answers[x]);
                    }
                }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Rohan
  • 1,312
  • 3
  • 17
  • 43
  • You almost always want to use `equals` to compare strings. – greedybuddha Jun 11 '13 at 16:34
  • If you are comparing strings you need to use `.equals()`, otherwise, it won't return true. – BlackHatSamurai Jun 11 '13 at 16:35
  • see: http://stackoverflow.com/questions/767372/java-string-equals-versus – DannyMo Jun 11 '13 at 16:35
  • 4
    Post a question about new technology, uml or an interesting algorithm, people won't even read it. Post a question about comparing Strings using `==` and everybody goes crazy for rep instead of voting to close for being a duplicated question. – Luiggi Mendoza Jun 11 '13 at 16:38

6 Answers6

4

In Java, String aren't primitive values, you have to use String.equals() to compare strings so, change this:

if(answerEntered[x] == answers[x])

to

if(answerEntered[x].equals(answers[x]))

I would also suggest that you check for nullability and ignore case, so:

String answer = answerEntered[x];
boolean isAnswerCorrect = 
        answer != null && 
            //remove trailling spaces and ignore case (a==A)
        answer.trim().equalsIgnoreCase(answers[x]);
if(isAnswerCorrect){
mrcaramori
  • 2,503
  • 4
  • 29
  • 47
  • Also, while you are doing you initial development/debugging - add some sort of debugging code so that you can verify that your input array is getting populated (if that is a concern). – JayDM Jun 11 '13 at 17:02
1

For String comparison, you need to use equals instead of ==, which for non-primitive data types, such as String, compares their references, not values.

String a = "foo";
String b = "bar";

if (a.equals(b))
{
    //doSomething
}
Steve P.
  • 14,489
  • 8
  • 42
  • 72
1

For String or any object-equality test in Java, you should almost always be using equals. The == operator only compares references when used with objects (but will work the way you expect it to with primitives like int, boolean, etc); that is, it checks to see if the operands both point/refer to the same object instance. What you're interested in doing is comparing the contents of the String instance, and equals will do that for you:

if(answerEntered[x].equals(answers[x])) {
   ...
}
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
1

The problem is in the comparison :

String a = "foo";
String b = "bar";

if (a.equals(b))
    //doSomething

AS it Has been answered before.

Extra information, in the for loop of the if / else you are looping only the first 3 positions, not the 5 that exists in the answerEntered array.

Cheers

0

Use .equals to compare strings. equals compares the values, where == compares the reference .

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
0

In Java, == comparison compares reference identity, means the two things you compare must be the same object. Two objects with the same values are treated as different.

You statement:

if(answerEntered[x] == answers[x])

The answerEntered contains string that is different with any string in answer even if they have the same value.

Java uses Object's .equals method to compare by value, i.e. Two objects are equal as long as they have the same value.

Changing:

if(answerEntered[x] == answers[x])

to

if(answerEntered[x].equals(answers[x])) 

should solve the problem.

Also, as answerEntered contains user inputed value, you'd better pre-process it before using it. For example, user might put answer "a " with spaces at the end. You might want to get rid of those spaces as well.

Otherwise "a " will be treated as an incorrect answer.

KKKCoder
  • 903
  • 9
  • 18