Results 1 to 19 of 19
- 05-05-2011, 05:47 AM #1
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
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(); } }
- 05-05-2011, 05:54 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
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.
- 05-05-2011, 05:59 AM #3
Advised the same here. Seems like this is just another duplicate thread for the same problem.
GoldestJava Is A Funny Language... Really!
Click on * and add to member reputation, if you find their advices/solutions effective.
- 05-05-2011, 06:02 AM #4
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
I am getting "not a statement" expecting ";" on this line
Java Code:}else (currentRoll < lastRoll){
- 05-05-2011, 06:10 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
- 05-05-2011, 06:13 AM #6
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,
GoldestJava Is A Funny Language... Really!
Click on * and add to member reputation, if you find their advices/solutions effective.
- 05-05-2011, 06:15 AM #7
- 05-05-2011, 06:16 AM #8
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
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
- 05-05-2011, 06:25 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
What is the exact new error?(copy/paste, the stack trace can be extremely helpful to us)
- 05-05-2011, 06:31 AM #10
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
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
- 05-05-2011, 06:36 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
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()); } }
I will try to look through your code in a few to see if I can figure out what you have wrong.
- 05-05-2011, 06:46 AM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
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(); } }
- 05-05-2011, 06:53 AM #13
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
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?
- 05-05-2011, 07:02 AM #14
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
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
- 05-05-2011, 07:09 AM #15
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
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.
- 05-05-2011, 07:37 AM #16
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
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.
- 05-05-2011, 08:19 AM #17
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
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?
- 05-05-2011, 08:42 AM #18
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
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.
- 05-05-2011, 09:04 AM #19
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
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
-
boolean even or odd?
By shazakala in forum New To JavaReplies: 3Last Post: 04-17-2011, 08:02 AM -
Boolean help
By DMarsh12 in forum New To JavaReplies: 8Last Post: 04-11-2011, 09:41 PM -
use boolean as 0 or 1
By joost_m in forum New To JavaReplies: 10Last Post: 04-13-2010, 11:22 AM -
Error: incompatible types, found: int required: boolean
By silvia in forum New To JavaReplies: 6Last Post: 10-08-2008, 08:09 AM -
boolean to string
By otoro_java in forum New To JavaReplies: 2Last Post: 01-30-2008, 05:31 AM
Bookmarks