Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Help! Variable potentially getting Overwritten!

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.
     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.");


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help! Variable potentially getting Overwritten!

    Quote Originally Posted by Norm View Post
    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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help! Variable potentially getting Overwritten!

    Integer, but my computers teacher says the opposite, if I'm reading you right.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help! Variable potentially getting Overwritten!

    Right, ran the System.out.printlns you suggested, and they didn't output anything.

    Any further suggestions?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help! Variable potentially getting Overwritten!

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help! Variable potentially getting Overwritten!

    Ok, I moved them. The answers match up with the solutions, but the score isn't being increased.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

     
        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
    Last edited by Norm; January 22nd, 2013 at 08:53 PM. Reason: change java to code in ending tag

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?
    System.out.println("Score: "+Score);
    Add something to different to each String to make each unique:
    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.
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    Hagstrom505 (January 26th, 2013)

Similar Threads

  1. What is the difference between instant variable and instance variable?
    By avistein in forum Java Theory & Questions
    Replies: 7
    Last Post: January 6th, 2013, 11:42 PM
  2. Replies: 0
    Last Post: October 30th, 2012, 10:06 AM
  3. How to compare variable with multiple variable?
    By mharck in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 11th, 2012, 09:02 AM
  4. For-loop: initialisation of variable, can't set variable to value zero?
    By Bitbot in forum Loops & Control Statements
    Replies: 4
    Last Post: July 15th, 2012, 02:32 PM
  5. Replies: 5
    Last Post: November 16th, 2011, 11:22 AM