Tuesday, June 11, 2013

If Else Statement Wont Work For Do While Loop - Java | Dream.In ...


Example
#{example}
"); ipb.editor_values.get('templates')['togglesource'] = new Template("
??? Cancel Source Edit
"); ipb.editor_values.get('templates')['toolbar'] = new Template(""); ipb.editor_values.get('templates')['button'] = new Template("
  • Icon
  • "); ipb.editor_values.get('templates')['menu_item'] = new Template("
  • #{title}
  • "); ipb.editor_values.get('templates')['togglesource'] = new Template("
    ??? Cancel Source Edit
    "); ipb.editor_values.get('templates')['emoticons_showall'] = new Template(""); ipb.editor_values.get('templates')['emoticon_wrapper'] = new Template("

    Emoticons

    "); // Add smilies into the mix ipb.editor_values.set( 'show_emoticon_link', false ); ipb.editor_values.set( 'bbcodes', $H({"snapback":{"id":"1","title":"Post Snap Back","desc":"This tag displays a little linked image which links back to a post - used when quoting posts from the board. Opens in same window by default.","tag":"snapback","useoption":"0","example":"[snapback]100[/snapback]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"topic":{"id":"5","title":"Topic Link","desc":"This tag provides an easy way to link to a topic","tag":"topic","useoption":"1","example":"[topic=1]Click me![/topic]","switch_option":"0","menu_option_text":"Enter the topic ID","menu_content_text":"Enter the title for this link","single_tag":"0","optional_option":"0","image":""},"post":{"id":"6","title":"Post Link","desc":"This tag provides an easy way to link to a post.","tag":"post","useoption":"1","example":"[post=1]Click me![/post]","switch_option":"0","menu_option_text":"Enter the Post ID","menu_content_text":"Enter the title for this link","single_tag":"0","optional_option":"0","image":""},"spoiler":{"id":"7","title":"Spoiler","desc":"Spoiler tag","tag":"spoiler","useoption":"0","example":"[spoiler]Some hidden text[/spoiler]","switch_option":"0","menu_option_text":"","menu_content_text":"Enter the text to be masked","single_tag":"0","optional_option":"0","image":""},"acronym":{"id":"8","title":"Acronym","desc":"Allows you to make an acronym that will display a description when moused over","tag":"acronym","useoption":"1","example":"[acronym='Laugh Out Loud']lol[/acronym]","switch_option":"0","menu_option_text":"Enter the description for this acronym (EG: Laugh Out Loud)","menu_content_text":"Enter the acronym (EG: lol)","single_tag":"0","optional_option":"0","image":""},"hr":{"id":"12","title":"Horizontal Rule","desc":"Adds a horizontal rule to separate text","tag":"hr","useoption":"0","example":"[hr]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"1","optional_option":"0","image":""},"php":{"id":"14","title":"PHP Code","desc":"Allows you to enter PHP code into a formatted/highlighted syntax box","tag":"php","useoption":"0","example":"[php]$variable = true;\n\nprint_r($variable);[/php]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"html":{"id":"15","title":"HTML Code","desc":"Allows you to enter formatted/syntax-highlighted HTML code","tag":"html","useoption":"0","example":"[html]\n \n[/html]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"sql":{"id":"16","title":"SQL Code","desc":"Allows you to enter formatted/syntax-highlighted SQL code","tag":"sql","useoption":"0","example":"[sql]SELECT p.*, t.* FROM posts p LEFT JOIN topics t ON t.tid=p.topic_id WHERE t.tid=7[/sql]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"xml":{"id":"17","title":"XML Code","desc":"Allows you to enter formatted/syntax-highlighted XML code","tag":"xml","useoption":"0","example":"[xml]\n \n Test\n \n[/xml]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"member":{"id":"31","title":"Member","desc":"Given a member name, a link is automatically generated to the member's profile","tag":"member","useoption":"1","example":"[member=skyhawk133] runs this site.","switch_option":"0","menu_option_text":"Input Username of Member","menu_content_text":"","single_tag":"1","optional_option":"0","image":"memberbbcode.png"},"extract":{"id":"33","title":"Extract Blog Entry","desc":"This will allow users to define an extract for an entry. Only this piece of the entry will be displayed on the main blog page and will show up in the RSS feed.","tag":"extract","useoption":"0","example":"[extract]This is an example![/extract]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"blog":{"id":"34","title":"Blog Link","desc":"This tag provides an easy way to link to a blog.","tag":"blog","useoption":"1","example":"[blog=100]Click me![/blog]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"entry":{"id":"35","title":"Blog Entry Link","desc":"This tag provides an easy way to link to a blog entry.","tag":"entry","useoption":"1","example":"[entry=100]Click me![/entry]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"twitter":{"id":"36","title":"Twitter","desc":"A tag to link to a user's twitter account","tag":"twitter","useoption":"0","example":"[twitter]userName[/twitter]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":"twitter.png"},"inline":{"id":"37","title":"Inline Code","desc":"Formats code inline instead of in a seperate code box. ","tag":"inline","useoption":"0","example":"[inline]style=\"font-size: 12px;\"[/inline]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"il":{"id":"38","title":"Abbreviated Inline (IL)","desc":"Abbreviated version of the [inline] tag. ","tag":"il","useoption":"0","example":"[il]Code Here[/il]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":"il2.png"},"code":{"id":"41","title":"Code","desc":"Allows you to enter general code","tag":"code","useoption":"1","example":"[code]$text = 'Some long code here';[/code]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"1","image":""}}) ); ipb.vars['emoticon_url'] = "http://cdn.dreamincode.net/forums/public/style_emoticons/default"; //Search Setup ipb.vars['search_type'] = 'forum'; ipb.vars['search_type_id'] = 32; ipb.vars['search_type_2'] = 'topic'; ipb.vars['search_type_id_2'] = 322728; //]]>

    29 Replies - 185 Views - Last Post: Yesterday, 09:58 PM Rate Topic: -----

    #1 streek405 ?Icon User is offline

    Reputation: 0

    • Posts: 36
    • Joined: 10-March 13

    Posted Yesterday, 07:05 PM

    
 //I am getting an error on line 29, stating "else without if"  //this program will ask you enter a number, it its too high or low, re-enter it  import java.util.Scanner;  class doWhile2{ 	 	public static void main(String[] args){ 		 		Scanner input = new Scanner(System.in); 		 		int i = (int)(Math.random() * 100); 		int number; 		 		// create do while  		do{ 			System.out.println("Enter an integer: "); 			number = input.nextInt(); 			 		} 		while(true); 			 			if (i == number){ 			System.out.println("The number you entered matches our number " + i); 			} 			break; 			 			else if (i > number){ 			System.out.println("The number you entered is too low, the difference is " + (i - number)" try again: "); 			} 			 			else { 			System.out.println("The number you entered is too high, the differnce is " (number - i)" try again: "); 			} 		 		 	} }    


    Is This A Good Question/Topic? 0

    Replies To: If else statement wont work for do while loop

    #2 schutzzz ?Icon User is online

    Reputation: 20

    • Posts: 60
    • Joined: 22-April 13

    Re: If else statement wont work for do while loop

    Posted Yesterday, 07:07 PM

    Remove the break or put it inside the if statement.

    This post has been edited by schutzzz: Yesterday, 07:08 PM


    #3 jon.kiparsky ?Icon User is offline

    Reputation: 5524

    • Posts: 8,925
    • Joined: 19-March 11

    Re: If else statement wont work for do while loop

    Posted Yesterday, 07:08 PM

    The else must follow the true branch directly. Here you have an intervening break instruction, which means the if statement has no else block. If the break is meant to be part of the true branch (if you want to break in the cases where i is equal to number) then it must go inside the curly braces.


    #4 streek405 ?Icon User is offline

    Reputation: 0

    • Posts: 36
    • Joined: 10-March 13

    Re: If else statement wont work for do while loop

    Posted Yesterday, 07:35 PM

    View Postjon.kiparsky, on 09 June 2013 - 07:08 PM, said:

    The else must follow the true branch directly. Here you have an intervening break instruction, which means the if statement has no else block. If the break is meant to be part of the true branch (if you want to break in the cases where i is equal to number) then it must go inside the curly braces.


    The reason why I put the break is so that if the user input a matching number, then the program should end. Am I thinking correctly? Also, I just removed the break, but now I am getting an error stating: "unexpected type" for the minus symbol in both my ELSE IF and ELSE statement. :(

    #5 jon.kiparsky ?Icon User is offline

    Reputation: 5524

    • Posts: 8,925
    • Joined: 19-March 11

    Re: If else statement wont work for do while loop

    Posted Yesterday, 07:37 PM

    View Postjon.kiparsky, on 09 June 2013 - 09:08 PM, said:

    If the break is meant to be part of the true branch (if you want to break in the cases where i is equal to number) then it must go inside the curly braces.



    #6 streek405 ?Icon User is offline

    Reputation: 0

    • Posts: 36
    • Joined: 10-March 13

    Re: If else statement wont work for do while loop

    Posted Yesterday, 07:39 PM

    View Postschutzzz, on 09 June 2013 - 07:07 PM, said:

    Remove the break or put it inside the if statement.


    But isnt the break so if my IF statement is true, then it will ignore the following statements? I've taken off the break and I added a + to my ELSE statement, but now I am getting the error: "unexpected type" for both my ELSE IF and ELSE statement, which is pointing to the subtraction sign...so now I have the following code:
    
  while(true); 			 			if (i == number){ 			System.out.println("The number you entered matches our number " + i); 			} 			 			 			else if (i > number){ 			System.out.println("The number you entered is too low, the difference is " + (i - number) " try again: "); 			} 			 			else { 			System.out.println("The number you entered is too high, the differnce is " + (number - i) " try again: "); 			}  


    #7 jon.kiparsky ?Icon User is offline

    Reputation: 5524

    • Posts: 8,925
    • Joined: 19-March 11

    Re: If else statement wont work for do while loop

    Posted Yesterday, 07:40 PM

    View Poststreek405, on 09 June 2013 - 09:35 PM, said:

    
 System.out.println("The number you entered is too low, the difference is " + (i - number)" try again: ");

    You're missing a plus sign.

    
 System.out.println("The number you entered is too low, the difference is " + (i - number) + " try again: ");


    #8 streek405 ?Icon User is offline

    Reputation: 0

    • Posts: 36
    • Joined: 10-March 13

    Re: If else statement wont work for do while loop

    Posted Yesterday, 07:45 PM

    View Postjon.kiparsky, on 09 June 2013 - 07:37 PM, said:

    View Postjon.kiparsky, on 09 June 2013 - 09:08 PM, said:

    If the break is meant to be part of the true branch (if you want to break in the cases where i is equal to number) then it must go inside the curly braces.


    Oh ok. I think I understand it now. But when I put the break inside the curly braces, I get an error stating "break outside switch or loop". So now I just omitted the break entirely.

    #9 jon.kiparsky ?Icon User is offline

    Reputation: 5524

    • Posts: 8,925
    • Joined: 19-March 11

    Re: If else statement wont work for do while loop

    Posted Yesterday, 07:46 PM

    View Poststreek405, on 09 June 2013 - 09:39 PM, said:

    But isnt the break so if my IF statement is true, then it will ignore the following statements?

    The syntax of the if statement is like this:

    if (CONDITION)
    STATEMENT BLOCK
    else STATEMENT BLOCK

    a STATEMENT BLOCK is eitehr a single statement:

    
 if (true) System.out.println("It's twue! It's twue!");

    which can be an empty statement:

    
 if (true) ;

    or some number of statements in curly braces:

    
 if (true) {   System.out.println("It's twue! It's twue!");   truthIsTrue = true;   int one = 1;   // etc. } 

    The STATEMENT BLOCK terminates after the closing curly brace (if you use braces) or after the semicolon terminating the single statement.

    Your break statement is not part of the if (and so, as written, it will always execute). Therefore, your else statements have no if to else themselves from, so the compiler complains.


    #10 streek405 ?Icon User is offline

    Reputation: 0

    • Posts: 36
    • Joined: 10-March 13

    Re: If else statement wont work for do while loop

    Posted Yesterday, 07:49 PM

    View Postjon.kiparsky, on 09 June 2013 - 07:40 PM, said:

    View Poststreek405, on 09 June 2013 - 09:35 PM, said:

    
 System.out.println("The number you entered is too low, the difference is " + (i - number)" try again: ");

    You're missing a plus sign.

    
 System.out.println("The number you entered is too low, the difference is " + (i - number) + " try again: ");

    Thank you, I too noticed that, but now I am getting the error "unexpected type" for both the ELSE IF and ELSE statement, pointing at the minus sign

    View Postjon.kiparsky, on 09 June 2013 - 07:46 PM, said:

    View Poststreek405, on 09 June 2013 - 09:39 PM, said:

    But isnt the break so if my IF statement is true, then it will ignore the following statements?

    The syntax of the if statement is like this:

    if (CONDITION)
    STATEMENT BLOCK
    else STATEMENT BLOCK

    a STATEMENT BLOCK is eitehr a single statement:

    
 if (true) System.out.println("It's twue! It's twue!");

    which can be an empty statement:

    
 if (true) ;

    or some number of statements in curly braces:

    
 if (true) {   System.out.println("It's twue! It's twue!");   truthIsTrue = true;   int one = 1;   // etc. } 

    The STATEMENT BLOCK terminates after the closing curly brace (if you use braces) or after the semicolon terminating the single statement.

    Your break statement is not part of the if (and so, as written, it will always execute). Therefore, your else statements have no if to else themselves from, so the compiler complains.


    Ok thank you, that actually makes sense!

    #11 Bountyhunter1234 ?Icon User is offline

    Reputation: 12

    • Posts: 64
    • Joined: 12-May 09

    Re: If else statement wont work for do while loop

    Posted Yesterday, 07:50 PM

    you start your while statement and immediately end it when you do this
    
while(true); 

    it should be

    
while(true){ 


    #12 streek405 ?Icon User is offline

    Reputation: 0

    • Posts: 36
    • Joined: 10-March 13

    Re: If else statement wont work for do while loop

    Posted Yesterday, 07:52 PM

    View PostBountyhunter1234, on 09 June 2013 - 07:50 PM, said:

    you start your while statement and immediately end it when you do this
    
while(true); 

    it should be

    
while(true){ 

    I originally made it like that, but then I got the error " ; expected", point that the curly brace

    #13 jon.kiparsky ?Icon User is offline

    Reputation: 5524

    • Posts: 8,925
    • Joined: 19-March 11

    Re: If else statement wont work for do while loop

    Posted Yesterday, 07:54 PM

    If you fix that missing plus sign, that will go away.

    Also, there's no reason why your break should be giving you an error if you put it inside the curly braces.

    See the following sample code. Try compiling it in a throwaway file:

    
public class Foo {    public static void main(String[] args)    {       int i = 0;       int number = 10;       System.out.println("The number you entered is too low, the difference is"             + (i - number) +" try again: ");        while (true) {          if (true)  { System.out.println ("true"); break;}       }    } } 

    This post has been edited by jon.kiparsky: Yesterday, 07:59 PM


    #14 Bountyhunter1234 ?Icon User is offline

    Reputation: 12

    • Posts: 64
    • Joined: 12-May 09

    Re: If else statement wont work for do while loop

    Posted Yesterday, 07:57 PM

    my fault i didn't realize it was a do-while, so forget my post i was looking at it as a while statement.


    #15 streek405 ?Icon User is offline

    Reputation: 0

    • Posts: 36
    • Joined: 10-March 13

    Re: If else statement wont work for do while loop

    Posted Yesterday, 08:03 PM

    View Postjon.kiparsky, on 09 June 2013 - 07:54 PM, said:

    If you fix that missing plus sign, that will go away.

    Also, there's no reason why your break should be giving you an error if you put it inside the curly braces.

    See the following sample code. Try compiling it in a throwaway file:

    
public class Foo {    public static void main(String[] args)    {       int i = 0;       int number = 10;       System.out.println("The number you entered is too low, the difference is"             + (i - number) +" try again: ");        while (true) {          if (true)  { System.out.println ("true"); break;}       }    } } 

    View PostBountyhunter1234, on 09 June 2013 - 09:50 PM, said:

    you start your while statement and immediately end it when you do this
    
while(true); 

    it should be

    
while(true){ 

    I've tried that but I am still getting the same error, for the break. This the the code

    
 while(true); 			 			if (true){ 			System.out.println("The number you entered matches our number " + i); 			break; 			} 			 			 			else if (i > number){ 			System.out.println("The number you entered is too low, the difference is " + (i - number) " try again: "); 			} 			 			else { 			System.out.println("The number you entered is too high, the differnce is " + (number - i) " try again: "); 			} 		  

    Good catch! That is in fact the problem.

    LOL That was my very first attempt that this code, but it stated that I need an ";" in place of the curly brace for the WHILE. Also, in my book, for the do while example, there is an ";" not a curly brace for the while part, although that does not make any sense to me...

    View PostBountyhunter1234, on 09 June 2013 - 07:57 PM, said:

    my fault i didn't realize it was a do-while, so forget my post i was looking at it as a while statement.


    It's all good, man.


    Source: http://www.dreamincode.net/forums/topic/322728-if-else-statement-wont-work-for-do-while-loop/

    After Christmas Sales 2012 Charles Durning Webster Ny Mcdonalds Restaurants Open on Christmas Day jessica simpson santa tracker

    No comments:

    Post a Comment

    Note: Only a member of this blog may post a comment.