java - Can if statements be nested in a while loop? -


i've been working code uses do-while loop, , wanted add if else statement loop. do-while loop checks see text user enters , finish if word 'exit' entered.

public static void main(string[] args) {      string endprogram = "exit";     string userinput;     java.util.scanner input = new java.util.scanner(system.in);      {          userinput = input.nextline();         if ( !userinput.equalsignorecase(endprogram) ) {             system.out.printf("you entered following: %s", userinput);         } else      } while ( !userinput.equalsignorecase(endprogram) );  } 

when try compile code, error command prompt saying:

sentinelexample.java:20: error: illegal start of expression             } while ( !userinput.equalsignorecase(endprogram) );             ^ sentinelexample.java:22: error: while expected       }        ^ sentinelexample.java:24: error: reached end of file while parsing }  ^ 

when remove if else statement in loop, program compiles fine. there wrong syntax in program? or not possible put if else statement in while loop?

check code here missing else block:

    userinput = input.nextline();     if ( !userinput.equalsignorecase(endprogram) ) {         system.out.printf("you entered following: %s", userinput);     } else  

this reason compilation error. can fix either removing else altogether or if want add in following:

    userinput = input.nextline();     if ( !userinput.equalsignorecase(endprogram) ) {         system.out.printf("you entered following: %s", userinput);     } else {         //     } 

and answer questions, yes it's valid nest if statements in while loop.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -