Results 1 to 7 of 7
Thread: Misunderstanding of Break
- 10-22-2008, 03:57 AM #1
Member
- Join Date
- Oct 2008
- Location
- Iowa
- Posts
- 2
- Rep Power
- 0
Misunderstanding of Break
I am just new to learning Java. In the text they show an example of using a break and label. I have tried it in my code but it is not working. Can anyone point me in the right direction.
Included is a copy of the section that is driving me crazy.
toHere: //"This is the label where I wish to jump to"
//"The error I get is the label is not valid
JOptionPane.showMessageDialog (null, intro);
String enterNumber = JOptionPane.showInputDialog (null, "PLEASE ENTER YOUR NUMBER",JOptionPane.QUESTION_MESSAGE);
int enteredValue = Integer.parseInt(enterNumber);
//The do while is used first to create a row of numbers incrementing
do {
//If the number is entered wrong it will advise the user and send the software back to the start.
if ((enteredValue < 10) || (enteredValue > 20)) {
// JOptionPane.showMessageDialog (null,"YOUR NUMBER SHOULD BE \n BETWEEN TEN AND TWENTY");
break toHere;
}
display += numb + gap; //the string is built up as the numbers add
System.out.println (display);
numb++;
} while (numb <= enteredValue);
-
This is probably not good programming practice. I would like to dissuade you from trying to use break and labels and instead use standard logic blocks and flow control statements. Otherwise you risk creating the dreaded spaghetti code.
For anyone interested, here's his code with code tags used to retain formatting:
Java Code:toHere: // "This is the label where I wish to jump to" // "The error I get is the label is not valid JOptionPane.showMessageDialog(null, "Intro"); String enterNumber = JOptionPane.showInputDialog(null, "PLEASE ENTER YOUR NUMBER", JOptionPane.QUESTION_MESSAGE); int enteredValue = Integer.parseInt(enterNumber); // The do while is used first to create a row of numbers incrementing do { // If the number is entered wrong it will advise the user and send the // software back to the start. if ((enteredValue < 10) || (enteredValue > 20)) { // JOptionPane.showMessageDialog (null,"YOUR NUMBER SHOULD BE \n BETWEEN // TEN AND TWENTY"); break toHere; } display += numb + gap; // the string is built up as the numbers add System.out.println(display); numb++; } while (numb <= enteredValue);Last edited by Fubarable; 10-22-2008 at 05:21 AM.
-
If you look at the Java Language Specification, the JLS, the section on the break statement you'll see this:
14.15 The break Statement
//....
A break statement with label Identifier attempts to transfer control to the enclosing labeled statement (§14.7) that has the same Identifier as its label; this statement, which is called the break target, then immediately completes normally. In this case, the break target need not be a while, do, for, or switch statement. A break statement must refer to a label within the immediately enclosing method or initializer block. There are no non-local jumps.
Your immediate problem is that you are trying to make a non-local jump. Your overriding problem however is that you are trying to make spaghetti code.Last edited by Fubarable; 10-22-2008 at 05:59 AM.
- 10-22-2008, 06:23 AM #4
@Fubarable is correct.
break is not a goto.
Goto considered harmful.
- 10-22-2008, 05:06 PM #5
Member
- Join Date
- Oct 2008
- Location
- Iowa
- Posts
- 2
- Rep Power
- 0
Thanks for the tip.
I was aware that a goto is bad and non-existent in Java. I guess what I need to do is move the label down to inside the do routine? Am I understanding 14.15 correctly?
- 10-22-2008, 05:24 PM #6
Senior Member
- Join Date
- Sep 2008
- Posts
- 135
- Rep Power
- 0
What you really need to do is forget about labels altogether. I've never used them, never seen them used in production code, and never met anybody who had any reasonable use for them. They just allow you to jump about your code willy-nilly and avoid having to think about OO properly
- 10-22-2008, 06:31 PM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I don't think so. Most of the industry level developments labels are used. It's really helpful.
Similar Threads
-
Break to certain class
By Arez in forum New To JavaReplies: 4Last Post: 10-20-2008, 04:13 AM -
Break Clock
By BruenorBH in forum Advanced JavaReplies: 20Last Post: 09-12-2008, 05:27 AM -
How to use Break with a label
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:45 PM -
How to use Break
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:45 PM -
Line break in tool tip..how??
By sandor in forum AWT / SwingReplies: 1Last Post: 05-16-2007, 01:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks