wood burning stoves 2.0*
The moose likes Java in General and the fly likes Single entry, single exit. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Single entry, single exit. " Watch "Single entry, single exit. " New topic
Author

Single entry, single exit.

Kyle Ross
Greenhorn

Joined: Jan 17, 2013
Posts: 2


My teacher wants us to enforce having one entry and one exit in a program. What do I put on that line to have the loop restart? Because you want to continue the program, but you didn't supply a name last time, so it can't drop down to the Output box. break; only exits the program.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Welcome to the Ranch.

You can either just put nothing there (at line 18), then line 22 will be executed and at line 24 it will jump back to line 7 for the next iteration of the loop.

You can also put continue; at line 18; then the program will immediately jump to line 7 for the next iteration (skipping the rest of the loop body, so line 22 will not be executed).

Note: The continueLoop variable in your code isn't doing anything useful. Note that the break; in line 15 jumps out of the loop, no matter what the value of continueLoop is.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3419
    
  12
Have you been specifically told that you have to put something on line 18 ? Because the way I'd do it is to put an else on line 21 (so that it didn't print Hello null) and then delete lines 15 - 18.

I'd also change line 12 to
if(usrOption == JOptionPane.YES_OPTION)
as it makes the code a lot clearer and you then no longer need the comment.


Joanne
Kyle Ross
Greenhorn

Joined: Jan 17, 2013
Posts: 2
Jesper de Jong wrote:
You can also put continue; at line 18; then the program will immediately jump to line 7 for the next iteration (skipping the rest of the loop body, so line 22 will not be executed).


Thank you, i have been looking for a while something that restarts a loop.

Note: The continueLoop variable in your code isn't doing anything useful. Note that the break; in line 15 jumps out of the loop, no matter what the value of continueLoop is.

But the break isn't hit until you say you want to exit the application. without the break and the continueLoop this wouldn't work.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Kyle Ross wrote:But the break isn't hit until you say you want to exit the application. without the break and the continueLoop this wouldn't work.

I didn't say "without the break". But the continueLoop variable is unnecessary. When you do a "break", the loop exits. The loop condition (what's in the brackets after the "while") is not checked.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7503
    
  18

Kyle Ross wrote:My teacher wants us to enforce having one entry and one exit in a program...

Kyle,

Please DontWriteLongLines (←click). I've broken them up this time, but please read the link and follow its advice.

Thanks.

Winston
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7503
    
  18

Kyle Ross wrote:Thank you, i have been looking for a while something that restarts a loop.

My advice: Don't.

Look to have your loop finish when (and ONLY when) it's supposed to.

BTW: There is a construct that us old-fashioned bods use that you may find useful:
Winston
Daryle Foulke
Greenhorn

Joined: Aug 27, 2009
Posts: 8

I also don't think this line is what your looking for



I think you want this. usrName is not going to be null if someone enter or the OK button with no name entered.




==============================
-Daryle
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Single entry, single exit.
 
Similar Threads
HOWTO check for click of Cancel button
Error incrementing value in catch block
Sombody please help this Noob!
Java 1b
help with joption pane cancel button