Results 1 to 19 of 19
  1. #1
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Question boolean error help when no boolean is given

    :confused:

    I am a getting boolean error on line 53 and 69 and a "not a statement" error "expecting ';'" on line 68. Could someone explain to me why that is please?


    this is the code starting and ending with the errors.

    Java Code:
          if (guess = JOptionPane.YES_OPTION){
          JOptionPane.showMessageDialog(null,
          "Your guess was right.  Your score has increased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              rightAnswer();
          }else {
          JOptionPane.showMessageDialog(null,
          "Your guess was right.  Your score has increased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              wrongAnswer();
          }
      }else (currentRoll < lastRoll){
          if (guess = JOptionPane.NO_OPTION){

    This is the full code so you won't be lost.


    Java Code:
    package Final;
    
    import javax.swing.*;
    
    public class P_Final_Exam {
        //declaring main variables
        static int currentRoll;
        static int lastRoll;
        static int score;
        static int guess;
    
        public static void main(String[] args){
    
        //ask player if they want to play
            int answer = JOptionPane.showConfirmDialog(null, "Do you want to play?",
                    "Dice Game",JOptionPane.YES_NO_CANCEL_OPTION,
        JOptionPane.QUESTION_MESSAGE);
    
            if (answer == JOptionPane.NO_OPTION){
                System.exit(1);
            } else if (answer == JOptionPane.CANCEL_OPTION){
                System.exit(1);
            }
            currentRoll = random();
            lastRoll = currentRoll;
    
            //JOptionPane.showMessageDialog(null, random());
            //System.out.println(System.getProperty("user.dir"));
    
        ImageIcon icon = new ImageIcon ("icons/" + currentRoll + ".png");
        Object[] options = {"Higher",
                        "Lower",
                        "Cancel"};
    
    guess = JOptionPane.showOptionDialog(null,
        "Will the next roll be higher?",
        "Dice Game",
        JOptionPane.YES_NO_CANCEL_OPTION,
        JOptionPane.QUESTION_MESSAGE,
        icon,
        options,
        options[0]);
    
    currentRoll = random();
    
      if (currentRoll == lastRoll){
          JOptionPane.showMessageDialog(null,
          "Your guess was neither correct nor incorrect.  Score will remain the same.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
      }else if (currentRoll > lastRoll){
          if (guess = JOptionPane.YES_OPTION){
          JOptionPane.showMessageDialog(null,
          "Your guess was right.  Your score has increased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              rightAnswer();
          }else {
          JOptionPane.showMessageDialog(null,
          "Your guess was right.  Your score has increased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              wrongAnswer();
          }
      }else (currentRoll < lastRoll){
          if (guess = JOptionPane.NO_OPTION){
          JOptionPane.showMessageDialog(null,
          "Your guess was wrong.  Your score has decreased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              rightAnswer();
          }else {
          JOptionPane.showMessageDialog(null,
          "Your guess was wrong.  Your score has decreased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              wrongAnswer();
          }
      }
      answer = JOptionPane.showConfirmDialog(null, "Do you want to play again?",
                    "Dice Game",JOptionPane.YES_NO_OPTION,
        JOptionPane.QUESTION_MESSAGE);
    
            if (answer == JOptionPane.NO_OPTION){
                System.exit(1);
            }
    } 
    public static int random(){
        //generate random number for die and icon
       int num;
       num = (int)(Math.random()*6) + 1;
    
       return num;
    }
    public static int rightAnswer(){
        //add to score
        score++;
        return score;
    }
    public static int wrongAnswer(){
        //subtract from score
        score--;
        return score;
    }
    public static int sameAnswer(){
        //do nothing to score
        return score;
    }
    public static int exit(){
        //display final score on exit
    
        return exit();
        }
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Can I see the exact error message? Copy/paste, not paraphrased.


    If it says ';' missing, you should consider looking around that line and scrutinize it. There is a good change you have a weird syntax error. Also, consider highlighting the line that caused the error too.

    Also, comparison is done with ==, not =. = is the assignment operator.

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by sunde887 View Post
    If it says ';' missing, you should consider looking around that line and scrutinize it. There is a good change you have a weird syntax error. Also, consider highlighting the line that caused the error too.
    Advised the same here. Seems like this is just another duplicate thread for the same problem.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    I am getting "not a statement" expecting ";" on this line

    Java Code:
     }else (currentRoll < lastRoll){
    just figured out about the == just a few min ago and was getting ready to post that when I saw your reply.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by goldest View Post
    Advised the same here. Seems like this is just another duplicate thread for the same problem.

    Goldest
    Haha, I just read your post, how interesting that we both recommended the same thing with different wording.

  6. #6
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    If you are evaluating the condition "(currentRoll < lastRoll)" then it should be inside an if statement. something like,
    Java Code:
    }else [COLOR="Red"]if[/COLOR] (currentRoll < lastRoll){

    Hope that makes some sense,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by sunde887 View Post
    how interesting that we both recommended the same thing with different wording.
    Haha! As they say, "Great minds think alike". LOL :D
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  8. #8
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    Could this " (currentRoll < lastRoll) " in that line of code be causing me my problem? I am going to try and work with it and take it out for now. I have come across a minor problem but one thing at a time right now.

    ************************************************** **************************************
    That did take out my error but now when I click on the lower button which is = to the no_option and it is correct it is telling me that I am wrong. I am losing my mind trying to get this done. If it isn't one thing, it is something else. I need this much of the code to work right before I go any further.

    current code is as follows:


    Java Code:
    package Final;
    
    import javax.swing.*;
    
    public class P_Final_Exam {
        //declaring main variables
        static int currentRoll;
        static int lastRoll;
        static int score;
        static int guess;
    
        public static void main(String[] args){
    
        //ask player if they want to play
            int answer = JOptionPane.showConfirmDialog(null, "Do you want to play?",
                    "Dice Game",JOptionPane.YES_NO_CANCEL_OPTION,
        JOptionPane.QUESTION_MESSAGE);
    
            if (answer == JOptionPane.NO_OPTION){
                System.exit(1);
            } else if (answer == JOptionPane.CANCEL_OPTION){
                System.exit(1);
            }
            currentRoll = random();
            lastRoll = currentRoll;
    
            //JOptionPane.showMessageDialog(null, random());
            //System.out.println(System.getProperty("user.dir"));
    
        ImageIcon icon = new ImageIcon ("icons/" + currentRoll + ".png");
        Object[] options = {"Higher",
                        "Lower",
                        "Cancel"};
    
    guess = JOptionPane.showOptionDialog(null,
        "Will the next roll be higher?",
        "Dice Game",
        JOptionPane.YES_NO_CANCEL_OPTION,
        JOptionPane.QUESTION_MESSAGE,
        icon,
        options,
        options[0]);
    
    currentRoll = random();
    
      if (currentRoll == lastRoll){
          JOptionPane.showMessageDialog(null,
          "Your guess was neither correct nor incorrect.  Score will remain the same.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
      }else if (currentRoll > lastRoll){
          if (guess == JOptionPane.YES_OPTION){
          JOptionPane.showMessageDialog(null,
          "Your guess was right.  Your score has increased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              rightAnswer();
          }else {
          JOptionPane.showMessageDialog(null,
          "Your guess was right.  Your score has increased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              wrongAnswer();
          }
      }else {
          if (guess == JOptionPane.NO_OPTION){
          JOptionPane.showMessageDialog(null,
          "Your guess was wrong.  Your score has decreased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              rightAnswer();
          }else {
          JOptionPane.showMessageDialog(null,
          "Your guess was wrong.  Your score has decreased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              wrongAnswer();
          }
      }
      answer = JOptionPane.showConfirmDialog(null, "Do you want to play again?",
                    "Dice Game",JOptionPane.YES_NO_OPTION,
        JOptionPane.QUESTION_MESSAGE);
    
            if (answer == JOptionPane.NO_OPTION){
                System.exit(1);
            }
    } 
    public static int random(){
        //generate random number for die and icon
       int num;
       num = (int)(Math.random()*6) + 1;
    
       return num;
    }
    public static int rightAnswer(){
        //add to score
        score++;
        return score;
    }
    public static int wrongAnswer(){
        //subtract from score
        score--;
        return score;
    }
    public static int sameAnswer(){
        //do nothing to score
        return score;
    }
    public static int exit(){
        //display final score on exit
    
        return exit();
        }
    }
    Last edited by drewtrcy; 05-05-2011 at 06:22 AM. Reason: eorr is gone, but now I have a logic error

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What is the exact new error?(copy/paste, the stack trace can be extremely helpful to us)

  10. #10
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    it isn't giving me an error, that is why I said it is a logic error. When I ran it it gave me a 6 so obviously I would choose Lower for my answer which was a correct answer but it told me it was wrong. I know that the "same" works with no problems. I know it has to do with how I wrote the code, but being a newbie at this I can't seem to figure it out right now.

    BTW, how do I do the stack trace, I have never done that before. I am using NetBeans.
    Last edited by drewtrcy; 05-05-2011 at 06:32 AM. Reason: forgot to include question

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    A stack trace is basically all the stuff thats sent out when you get an exception. If you want to see real quick, try this.

    Java Code:
    public class X{
      public static void main(String[] args){
        Object o;
        System.out.println(o.toString());
      }
    }
    This should throw a null pointer exception and a small stack trace which shows where the initial exception was generated and how it percolated from the line of code that caused it out to the console.

    I will try to look through your code in a few to see if I can figure out what you have wrong.

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Java Code:
    else if (currentRoll > lastRoll){
          if (guess == JOptionPane.YES_OPTION){
          JOptionPane.showMessageDialog(null,
          "Your guess was right.  Your score has increased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              rightAnswer();
          }else {
          JOptionPane.showMessageDialog(null,
          "Your guess was right.  Your score has increased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              wrongAnswer();
          }
      }else {
          if (guess == JOptionPane.NO_OPTION){
          JOptionPane.showMessageDialog(null,
          "Your guess was wrong.  Your score has decreased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              rightAnswer();
          }else {
          JOptionPane.showMessageDialog(null,
          "Your guess was wrong.  Your score has decreased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              wrongAnswer();
          }
      }
    Check this segment. If the current roll > last roll both messages say they increase the score(even though one decreases it), you have a similar problem in the else clause(this time, both say decreased score). The scores are both changed correctly, just the dialog displays the incorrect text for some.

  13. #13
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    Thank you for that, sometimes it takes more than my 4 eyes to see something (I am legally blind and have glasses, lol). now that is fixed, how do I get it to return to the top an infinite amount of times until the player quits the game?

  14. #14
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    I just googled this, would it be practical for my use or would you suggest something else

    Infinite For loop Example | Java Examples - Java Program Sample Source Code

  15. #15
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    That works, however some people don't like to use a true infinite loop. Also, if you are gonna loop x times where x is an unknown, you may want to consider a do while or a while loop. From there just loop while some value is not input by the user.

  16. #16
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    how would I do that when there is no actual input from the user, all they do is point and click. We did go over loops in class but didn't do much at all with the do...while loops

    I just tried that for loop and it didn't do exactly what I wanted it to do. I placed it right above the higher or lower question and it just kept repeating that little section and would go forward.

  17. #17
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    better yet, since I already am asking if they want to play again, how do I get it to return to the top of the roll where it asks if the next guess is higher or lower?

  18. #18
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    I have the loop working now but I am back to the icon, it is there but not changing. What can I do to change it? If I get it figured out I will let y'all know.

  19. #19
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    My icon does change now, but not when I need it too. I need it to change on the next dialog box when the user clicks on higher or lower but it doesn't change until it gets to the next guess of higher or lower

    here is the current code


    Java Code:
    package Final;
    
    import javax.swing.*;
    
    public class P_Final_Exam {
        //declaring main variables
        static int currentRoll;
        static int lastRoll;
        static int score;
        static int guess;
    
        public static void main(String[] args){
    
        //ask player if they want to play
            int answer = JOptionPane.showConfirmDialog(null, "Do you want to play?",
                    "Dice Game",JOptionPane.YES_NO_CANCEL_OPTION,
        JOptionPane.QUESTION_MESSAGE);
    
            if (answer == JOptionPane.NO_OPTION){
                System.exit(1);
            } else if (answer == JOptionPane.CANCEL_OPTION){
                System.exit(1);
            }
    
            //JOptionPane.showMessageDialog(null, random());
            //System.out.println(System.getProperty("user.dir"));
    for(;;){
    currentRoll = random();
    
        ImageIcon icon = new ImageIcon ("icons/" + currentRoll + ".png");
        Object[] options = {"Higher",
                        "Lower",
                        "Cancel"};
    
    guess = JOptionPane.showOptionDialog(null,
        "Will the next roll be higher?",
        "Dice Game",
        JOptionPane.YES_NO_CANCEL_OPTION,
        JOptionPane.QUESTION_MESSAGE,
        icon,
        options,
        options[0]);
    
          if (guess == JOptionPane.CANCEL_OPTION){
              exit();
                System.exit(1);
          }else if (currentRoll == lastRoll){
          JOptionPane.showMessageDialog(null,
          "Your guess was neither correct nor incorrect.  Score will remain the same.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
      }else if (currentRoll > lastRoll){
          if (guess == JOptionPane.YES_OPTION){
          JOptionPane.showMessageDialog(null,
          "Your guess was right.  Your score has increased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              rightAnswer();
          }else {
          JOptionPane.showMessageDialog(null,
          "Your guess was wrong.  Your score has decreased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              wrongAnswer();
          }
      }else {
          if (guess == JOptionPane.NO_OPTION){
          JOptionPane.showMessageDialog(null,
          "Your guess was right.  Your score has increased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              rightAnswer();
          }else {
          JOptionPane.showMessageDialog(null,
          "Your guess was wrong.  Your score has decreased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              wrongAnswer();
          }
      }
      answer = JOptionPane.showConfirmDialog(null, "Do you want to play again?",
                    "Dice Game",JOptionPane.YES_NO_OPTION,
        JOptionPane.QUESTION_MESSAGE);
    
            if (answer == JOptionPane.NO_OPTION){
                exit();
            }else{
          lastRoll = currentRoll;
            }
        }
    } 
    public static int random(){
        //generate random number for die and icon
       int num;
       num = (int)(Math.random()*6) + 1;
    
       return num;
    }
    public static int rightAnswer(){
        //add to score
        score++;
        return score;
    }
    public static int wrongAnswer(){
        //subtract from score
        score--;
        return score;
    }
    public static int sameAnswer(){
        //do nothing to score
        return score;
    }
    public static int exit(){
        //display final score on exit
        JOptionPane.showMessageDialog(null,
          "Thank you for playing.  Your score is " + score +".",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE);
           System.exit(1);
        return exit();
        }
    }

Similar Threads

  1. boolean even or odd?
    By shazakala in forum New To Java
    Replies: 3
    Last Post: 04-17-2011, 08:02 AM
  2. Boolean help
    By DMarsh12 in forum New To Java
    Replies: 8
    Last Post: 04-11-2011, 09:41 PM
  3. use boolean as 0 or 1
    By joost_m in forum New To Java
    Replies: 10
    Last Post: 04-13-2010, 11:22 AM
  4. Replies: 6
    Last Post: 10-08-2008, 08:09 AM
  5. boolean to string
    By otoro_java in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 05:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •