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 6 of 6

Thread: do while syntax error problem

  1. #1
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default do while syntax error problem

    im having trouble figuring out why its giving me a syntax error

    package DamageCalc;
     
    import java.util.Scanner;
     
    public class DmgCalc {
    	public static void main (String[] args) {
     
    		int attack,element;
    		boolean retry;
    		boolean yes = true;
    		String weak = "Your Damage against your weak element is ";
     
    		Scanner input = new Scanner(System.in);
    		do {
    		System.out.println("Enter your Attack");
    		attack = input.nextInt();
     
    		System.out.println("What is your Element? (Use 1-4) 1-Fire 2-Water 3-Earth 4-Wind");
    		element = input.nextInt();
     
    		if(attack == 0) {
    			System.out.println("Your damage is 0.");
    		}else{
    		switch(element) {
    		case 1:
    			attack =(int)((attack * 1.4 + 0 - 2 * 0.98) * 1.6);
    			System.out.println(weak + attack);
    		    break;
    		case 2:
    			attack =(int)((attack * 1.4 + 0 - 2 * 0.98) * 1.3);
    			System.out.println(weak + attack);
    		    break;
    		case 3:
    			attack =(int)((attack * 1.4 + 0 - 2 * 0.98) * 1.4);
    			System.out.println(weak + attack);
    		    break;
    		case 4:
    			element =(int)((attack * 1.4 + 0 - 2 * 0.98) * 1.3);
    			System.out.println(weak + attack);
    		    break;
    		 default:
    		    	System.out.println("Exiting...Please follow directions.");
    		    	break;
    		    }
    		System.out.println("Would you like to try another calculation?");
    		      retry = input.nextBoolean();
    		} while (retry = yes);
    	} 
    }
    }

    and the error i get is

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Syntax error, insert "while ( Expression ) ;" to complete DoStatement

    at DamageCalc.DmgCalc.main(DmgCalc.java:47)


    i dont understand because on the line above it i have the while ()
    the part im trying to add is the do while loop the rest of the code without it works nice i tried to set retry as string boolean int lol
    im really not sure if im doing this right though im all confused right now lol any help would be great


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: do while syntax error problem

    Are the {}s matched?

    Look at the condition in the while()? Is it a comparison or an assignment?
    Last edited by Norm; June 30th, 2011 at 09:07 PM.

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

    derekxec (July 1st, 2011)

  4. #3
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: do while syntax error problem

    thanks for replying i took out the messed up } and now ive narrowed it down to these things being the problem guess i need to learn the while and boolean and such better

    boolean retry;
    	boolean yes = true;
    System.out.println("Would you like to try another calculation?");
    		      yes = input.nextBoolean();
    		} while (yes = true);

    that does what i want it to except when its supposed to loop back it stops and gives this error lol

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextBoolean(Unknown Source)
    at DamageCalc.DmgCalc.main(DmgCalc.java:46)
    Last edited by derekxec; June 30th, 2011 at 11:22 PM.

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: do while syntax error problem

    You are still using assignment and not comparison. Besides when using booleans you do not need to compare.
    boolean tt = true;
    boolean ff = false;
    if(tt)  // when true
     
    if( ! ff) //when false

  6. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: do while syntax error problem

    Another thing, I wouldn't be using nextBoolean. That relies on the user typing "true" or "false". Whereas it is more usual to enter "yes" or "no". This also allows more flexibility as you can do an equalsIgnoreCase for "YES', "Yes" etc.

  7. The Following User Says Thank You to Junky For This Useful Post:

    derekxec (July 1st, 2011)

  8. #6
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: do while syntax error problem

    thank you guys very much i dont know what i was thinking lol next time i don't try to mess with it while i am falling asleep haha...i need to learn more about scanner stuff and boolean

    i appreciate all the help

Similar Threads

  1. Syntax error on token ";", @ expected after this token
    By MagicMojo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 16th, 2011, 07:48 AM
  2. syntax question
    By surfbumb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 9th, 2011, 04:01 PM
  3. How do you read this syntax?
    By meowCat in forum Java Theory & Questions
    Replies: 5
    Last Post: August 8th, 2010, 03:09 PM
  4. New Syntax Highlighting Feature!
    By JavaPF in forum Forum Updates & Feedback
    Replies: 15
    Last Post: July 14th, 2010, 09:20 AM