Dan Grueneberg
Dan Grueneberg

Reputation: 215

Java if statement not comparing to the variable

This is another school project where the main class generates two random numbers and then displays them as an addition problem. The user then has to enter the sum of the two numbers. The program should compare the input to the correct answer but every time the correct answer is put in, it says it is wrong?

Main Class:

import java.util.Random;

public class MathQuiz
{
private int answer1, answer2, answer3, answer4, answer5, n1, n2, sum, score,           scoreFinal;
private String message, m2, m3, m4, m5, finalPage;
public MathQuiz()
{
}

Random dice1 = new Random();

public void setN1()
{
    n1 = dice1.nextInt(100);
}

public void setN2()
{
    n2 = dice1.nextInt(100);
}

public void setSum()
{
    sum = n1+ n2;
}

public int getN1()
{
    return n1;
}

public int getN2()
{
    return n2;
}

public int getSum()
{
    return sum;
}



public String getAnswer1(String a1)
{
    if (a1.equals(getSum()))
    {
        message = "Correct";
        score++;
    }

    else 
    {
        message = "Incorrect";
    }

    return message;
}

public String getAnswer2(String a2)
{
    if (a2.equals(getSum()))
    {
        m2 = "Correct";
        score++;
    }

    else
    {
        m2 = "Incorrect";
    }

    return m2;
}

public String getAnswer3(String a3)
{
    if (a3.equals(getSum()))
    {
        m3 = "Correct";
        score++;
    }

    else
    {
        m3 = "Incorrect";
    }

    return m3;
}

public String getAnswer4(String a4)
{
    if (a4.equals(getSum()))
    {
        m4 = "Correct";
        score++;
    }

    else
    {
        m4 = "Incorrect";
    }

    return m4;
}

public String getAnswer5(String a5)
{
    if (a5.equals(getSum()))
    {
        m5 = "Correct";
        score++;
    }

    else
    {
        m5 = "Incorrect";
    }

    return m5;
}

public String getLast()
{
    scoreFinal = score * 20;
    finalPage = "Finished! Your score is " + scoreFinal + "%"; 
    return finalPage;
}
}

JOptionPane:

import javax.swing.JOptionPane;

public class Dialog
{
public static void main(String[] args)
{
    String A1, A2, A3, A4, A5;
    int a1, a2, a3, a4, a5;

    MathQuiz quiz = new MathQuiz();

    JOptionPane.showMessageDialog(null, "Answer these 5 math questions...");

    quiz.setN1();
    quiz.setN2();
    quiz.setSum();
    A1 = JOptionPane.showInputDialog(quiz.getN1() + " + " + quiz.getN2());

    JOptionPane.showMessageDialog(null, quiz.getAnswer1(A1));

    quiz.setN1();
    quiz.setN2();
    quiz.setSum();
    A2 = JOptionPane.showInputDialog(quiz.getN1() + " + " + quiz.getN2());

    JOptionPane.showMessageDialog(null, quiz.getAnswer1(A2));

    quiz.setN1();
    quiz.setN2();
    quiz.setSum();
    A3 = JOptionPane.showInputDialog(quiz.getN1() + " + " + quiz.getN2());

    JOptionPane.showMessageDialog(null, quiz.getAnswer1(A3));

    quiz.setN1();
    quiz.setN2();
    quiz.setSum();
    A4 = JOptionPane.showInputDialog(quiz.getN1() + " + " + quiz.getN2());

    JOptionPane.showMessageDialog(null, quiz.getAnswer1(A4));

    quiz.setN1();
    quiz.setN2();
    quiz.setSum();
    A5 = JOptionPane.showInputDialog(quiz.getN1() + " + " + quiz.getN2());

    JOptionPane.showMessageDialog(null, quiz.getAnswer1(A5));

    JOptionPane.showMessageDialog(null, quiz.getLast());
}
}

Upvotes: 0

Views: 888

Answers (5)

Pete B.
Pete B.

Reputation: 3276

At the time of this writing there are two answers, both of which are correct. First good job for trying something. Many come here looking for pros to do their homework for them.

Here are some things to think about to improve your code:

1) Consistent indentation will help you read your code. 2) Why do you need so many getAnswer methods, you only need one, not 1-5. 3) You should check if the String parameter to your getAnswer method is null, otherwise you will get a null pointer exception.

There are some other things, but that will work for now.

Upvotes: 0

vwvan
vwvan

Reputation: 457

Step 1: Instead of having the user ask a set of five questions, ask them one question. This will speed debugging immensely. Once your code works, make it five questions.

Step 2: With the code asking only one question you can then do the simpler change, Integer.toString() and compute the answer, as Asaph mentioned. You won't feel the performance hit.

Upvotes: 0

Asaph
Asaph

Reputation: 162781

The answers passed into the getAnswerX() methods are Strings and you're comparing them to integers. The String classes' .equals() method is returning false because the the argument is a different type, an int primitive. You can either convert the Strings to Integers using Integer.parseInt() or convert the ints to Strings by using Integer.toString() before comparing them.

Something like this should work better:

public String getAnswer1(String a1)
{
    if (Integer.parseInt(a1) == getSum())
    {
        message = "Correct";
        score++;
    }

    else 
    {
        message = "Incorrect";
    }

    return message;
}

Upvotes: 4

Noob
Noob

Reputation: 2927

you are trying to compare integer with a string

try to change

a1.equals(getSum())

to

a1.equals(String.valueOf(getSum()))

http://www.tutorialspoint.com/java/java_string_valueof.htm

Upvotes: 0

Fls'Zen
Fls'Zen

Reputation: 4654

You're comparing a String to an int in your code, which are not equals. Try changing your comparisons from

a1.equals(getSum())

to

a1.equals(getSum().toString())

in order to compare a string to a string.

Upvotes: 0

Related Questions