Help! Variable potentially getting Overwritten!
I'm working on a school project, which is basically a quiz type program, but I've hit a bit of a snag...
In it, I have a counter for the number of correct answers, but it doesn't store the value of this variable (score). It keeps going back to the value I indexed at the beginning as a base, 0.
Code java:
if (totalquestions==1){
txta_display.setText("Does not make difference");
if (answer==solutions[0]) {
score=score+1;
System.out.println("Score: "+Score);
}
}
else if (totalquestions==2){
txta_display.setText("Unrelated");
if (answer==solutions[1]){
score+1;
}
}
else if (totalquestions==3){
txta_display.setText("Not needed");
if (answer==solutions[2]){
score=score+1;
}
}
else if (totalquestions==4){
txta_display.setText("Won't help solve problem");
if (answer==solutions[3]){
score=score+1;
}
}
else if(totalquestions==5) {
txta_display.setText("Unimportant to answer");
if (answer==solutions[4]){
score= score+1;
}
}
else if(totalquestions>5){
txta_display.setText("You have finished! Your score is"+score+"out of 5.");
Re: Help! Variable potentially getting Overwritten!
Can you post the program's output that shows what you are talking about?
I don't see where you set the value of score to 0.
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Re: Help! Variable potentially getting Overwritten!
Quote:
Originally Posted by
Norm
Can you post the program's output that shows what you are talking about?
I don't see where you set the value of score to 0.
Sorry about that. I declared it globally up at the beginning of the program.
As for program's output, with several System.out.println statements I omitted from up above, I get the following output:
Score: 0(Starting value)
Score: 0(after first question)
Score: 0(second question, so on)
Score: 0
Score: 0
Re: Help! Variable potentially getting Overwritten!
Why isn't 0 a valid value? The value of score is only changed if one of the if statements is true.
If they are all false, the value won't be changed. Which if statement(s) are true?
Try debugging the code by adding println statements to print out the value of answer and the value of contents of the solutions array.
The Array class's toString() method will make printing array contents easier:
Code :
System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
Re: Help! Variable potentially getting Overwritten!
Zero is an okay response value, but this is when I run through the program using the correct answers, so all the if statements are considered true.
Thanks though, will try that.
Re: Help! Variable potentially getting Overwritten!
What data type is answer? If it's an object, you need to use the equals() method not == to compare it.
== is for primitives like int.
Re: Help! Variable potentially getting Overwritten!
Integer, but my computers teacher says the opposite, if I'm reading you right.
Re: Help! Variable potentially getting Overwritten!
Integer is a class. But sometimes the compiler will automatically extract its contents and retrieve the int value it holds using a hidden process called unboxing.
Re: Help! Variable potentially getting Overwritten!
Right, ran the System.out.printlns you suggested, and they didn't output anything.
Any further suggestions?
Re: Help! Variable potentially getting Overwritten!
Quote:
they didn't output anything
That would mean that they were not executed. Move them outside of any if statements so that they are sure to execute. Like before the first if
and after the last else.
Re: Help! Variable potentially getting Overwritten!
Ok, I moved them. The answers match up with the solutions, but the score isn't being increased.
Re: Help! Variable potentially getting Overwritten!
Can you post what was printed out and the code that did the printing?
The printout should show the values of: answer, totalquestions and the full contents of solutions.
Did you also consider the value of totalquestions?
Re: Help! Variable potentially getting Overwritten!
Total questions isn't an issue. I already fixed that problem, it's in a different section of the code, so it wasn't part of the printout.
Code java:
System.out.println("answer:"+answer);
System.out.println("solution:"+ java.util.Arrays.toString(solutions));
buttonPressA();
buttonPressB();
buttonPressC();
buttonPressD();
int Score=0;
if (totalquestions.equals(1)){
txta_display.setText("What is the slope of a line containing (4,2) and (8,4)?\n A. 2\n B. 3\n C. 13\n D. 1");
if (answer==solutions[0]) {
score=score+1;
}
System.out.println("Score: "+Score);
}
else if (totalquestions.equals(2)){
txta_display.setText("Question");
if (answer==solutions[1]){
score=score+1;
}
System.out.println("Score: "+Score);
}
else if (totalquestions.equals(3)){
txta_display.setText("Question");
if (answer==solutions[2]){
score=score+1;
}
System.out.println("Score: "+Score);
}
else if (totalquestions.equals(4)){
txta_display.setText("Question");
if (answer==solutions[3]){
score=score+1;
}
System.out.println("Score: "+Score);
}
else if(totalquestions.equals(5)) {
txta_display.setText("Question");
if (answer==solutions[4]){
score=score+1;
}
System.out.println("Score: "+Score);
}
else if(totalquestions>5){
txta_display.setText("You have finished! Your score is"+score+"out of 5.");
}
Printout is as follows:
answer:null
solution:[1, 2, 4, 3, 4]
Score: 0
answer:1
solution:[1, 2, 4, 3, 4]
Score: 0
answer:2
solution:[1, 2, 4, 3, 4]
Score: 0
answer:4
solution:[1, 2, 4, 3, 4]
Score: 0
answer:3
solution:[1, 2, 4, 3, 4]
Score: 0
answer:4
solution:[1, 2, 4, 3, 4]
0
Re: Help! Variable potentially getting Overwritten!
You forgot to print out the value of totalquestions. Its value controls what solutions element answer is compared against.
If answer can have a null value then it is an object and you should use a method to compare it, not the == operator.
You have this same print out several places. How do you know which one printed?
Code :
System.out.println("Score: "+Score);
Add something to different to each String to make each unique:
Code :
System.out.println("1 Score: "+Score);
when posting code Please wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.