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!!
switch-case. it will make your code much simplierprovincescollection, so 'yes' it looks like you check if the written answer is somewhere in there and not if it the correct answer.