Results 1 to 7 of 7
  1. #1
    Steve Whalen is offline Member
    Join Date
    Oct 2008
    Location
    Iowa
    Posts
    2
    Rep Power
    0

    Default 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);

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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 06:21 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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 06:59 AM.

  4. #4
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    @Fubarable is correct.

    break is not a goto.
    Goto considered harmful.

  5. #5
    Steve Whalen is offline Member
    Join Date
    Oct 2008
    Location
    Iowa
    Posts
    2
    Rep Power
    0

    Default

    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?

  6. #6
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by Steve Whalen View Post
    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?
    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

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Break to certain class
    By Arez in forum New To Java
    Replies: 4
    Last Post: 10-20-2008, 05:13 AM
  2. Break Clock
    By BruenorBH in forum Advanced Java
    Replies: 20
    Last Post: 09-12-2008, 06:27 AM
  3. How to use Break with a label
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:45 PM
  4. How to use Break
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:45 PM
  5. Line break in tool tip..how??
    By sandor in forum AWT / Swing
    Replies: 1
    Last Post: 05-16-2007, 02:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •