0

Within my submit button, the if/else if statement is not working properly for my correct/incorrect output.

For example, question: where is Victoria? Answer: British Columbi - Incorrect. However if I answer: Alberta or British Columbia it is correct. I don't understand the error. The error occurs if I write the province incompletely, not if it's the wrong province.

How can I solve this?

Here's my code:

Submit button code

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             

    outputDegree.setText(null);
    if (inputAnswer.getText().equals(provinces.get(0))) {
     outputDegree.setText("Correct");
     nextQuestion.setEnabled(true);

    }
   else if (inputAnswer.getText().equals(provinces.get(1))) {
     outputDegree.setText("Correct");
     nextQuestion.setEnabled(true);
   }
   else if (inputAnswer.getText().equals(provinces.get(2))) {
     outputDegree.setText("Correct");
     nextQuestion.setEnabled(true);
   }
   else if (inputAnswer.getText().equals(provinces.get(3))) {
     outputDegree.setText("Correct");
     nextQuestion.setEnabled(true);
   }
   else if (inputAnswer.getText().equals(provinces.get(4))) {
     outputDegree.setText("Correct");
     nextQuestion.setEnabled(true);
   }
   else if (inputAnswer.getText().equals(provinces.get(5))) {
     outputDegree.setText("Correct");
     nextQuestion.setEnabled(true);
   }
   else if (inputAnswer.getText().equals(provinces.get(6))) {
     outputDegree.setText("Correct");
     nextQuestion.setEnabled(true);
   }
   else if (inputAnswer.getText().equals(provinces.get(7))) {
     outputDegree.setText("Correct");
     nextQuestion.setEnabled(true);
   }
   else if (inputAnswer.getText().equals(provinces.get(8))) {
     outputDegree.setText("Correct");
     nextQuestion.setEnabled(true);
   }
   else if (inputAnswer.getText().equals(provinces.get(9))) {         
     outputDegree.setText("Correct");
     nextQuestion.setEnabled(true);
   }
   else if (inputAnswer.getText().equals(provinces.get(10))) {         
     outputDegree.setText("Correct");
     nextQuestion.setEnabled(true);
   }
   else if (inputAnswer.getText().equals(provinces.get(11))) {
     outputDegree.setText("Correct");
     nextQuestion.setEnabled(true);
   }
   else if (inputAnswer.getText().equals(provinces.get(12))) {
     outputDegree.setText("Correct");
     nextQuestion.setEnabled(true);
   }
   else {
       outputDegree.setText("Incorrect, try again!!");
       nextQuestion.setEnabled(false);
    }

Displaying answer and possible answers (1 right, 3 wrong)

private void nextQuestionActionPerformed(java.awt.event.ActionEvent evt) {                                             
   boolean trueOrFalse;

   outputTextQuestion.setText(null);
   outputTextQuestion.setText(setQuestions().toString());
   outputAnswers.setText(null);        
   inputAnswer.setText(null);       
   outputDegree.setText(null);

   clicked++;

   int buttonLimit = 4;

   if (clicked  <= buttonLimit) {


       int correctAnswer = -1;

        for (int x = 0; x < cities.size(); x++)
        {
            if (trueOrFalse = outputTextQuestion.getText().contains(cities.get(x)))
            {
                correctAnswer = x;
                break;
            }
        }
        randomAnswers = new ArrayList <Integer>();
        Collections.addAll(randomAnswers, correctAnswer);

        for (int x=0; x < 3; x++)
        {
           int r = correctAnswer;
           while (randomAnswers.contains(r))
            {
                r = ((int)(Math.random()*100))%cities.size();

            }
            Collections.addAll(randomAnswers, r);
        }

        Collections.shuffle(randomAnswers);
        outputAnswers.setText(null);

        for (int r=0; r<randomAnswers.size(); r++) {
             int hint = randomAnswers.get(r);
             outputAnswers.append(provinces.get(hint) + "\n");
        }
}

   else{
        nextQuestion.setEnabled(false);
        submitButton.setEnabled(false);
        outputTextQuestion.setText("Start new round");
        outputDegree.setText(null);


   }

I am also using ArrayLists such as cities, provinces, randomAnswers. I just haven't shown my whole program because it's big. If needed, I can.

I'm doing this in Netbeans, and this program is a GUI

Thanks in advance!!

5
  • 1
    try to use switch-case. it will make your code much simplier Commented Jun 13, 2017 at 22:11
  • @dehasi But, it would still be comparing the same things? Wouldn't it? Commented Jun 13, 2017 at 22:13
  • 1
    You're comparing the written answer to 13 entries of the provinces collection, so 'yes' it looks like you check if the written answer is somewhere in there and not if it the correct answer. Commented Jun 13, 2017 at 22:13
  • note that that code could be drastically simpified with a loop Commented Jun 13, 2017 at 22:18
  • You say you don't understand the error; what is the error? Commented Jun 14, 2017 at 0:16

3 Answers 3

1

Not knowing which part of code is supposed to do what you seem to expect, where you expect a partial input to be a match, here are reasons:

  • In the first method, you're using equals(). A partial match will never equal the full text.

  • In the second method, you're using contain(), but reversed:
    getText().contains(cities.get(x))
    The partial text bert does not contain the full text Alberta. It's the other way, i.e. Alberta contains the text bert.

Sign up to request clarification or add additional context in comments.

Comments

0

You should really consider using a loop to iterate your list instead of doing it manually. Imagine if you have 50 elements in provinces.

String answer = inputAnswer.getText();
boolean isCorrect = false;
for (String city : provinces) {
    if (answer.equalsIgnoreCase(city)) {
        outputDegree.setText("Correct");
        nextQuestion.setEnabled(true);
        isCorrect = true;
        break; // stops the loop if answer is correct
    }
}
if (!isCorrect) { //if there were no correct answers
    outputDegree.setText("Incorrect, try again!!");
     nextQuestion.setEnabled(false);
}

Comments

0

In submitButtonActionPerformed() you are comparing inputAnswer.getText() with all the valid provinces - If it matches any of them you are setting outputDegree.setText("Correct") - Thus in your logic you interpret incorrect answer ony if the user input is not matching any of the provinces. You should think about this logic and correlate the question with user's answer in this API

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.