Results 1 to 4 of 4
  1. #1
    louist is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default How do you properly use break?

    Hello Again!

    I'm getting the error, "break outside switch or loop," with the following code:

    Java Code:
        //User Input and Validation Method (Input (from user): decimal; Return: decimal):
        public static int getInput(int decimal) {
            Scanner input = new Scanner(System.in);
            
            //While Loop to test for valid input:
            while (decimal > 0); {
                //decimal = User Input:
                            System.out.println("Please input a number from 1 to 65535, or a negative number to quit:");
                decimal = input.nextInt();
                           
                //If Statement to Check Validity; Return only on Valid decimal, or Termnimate decimal:
                if (decimal > 65535) {
                    System.out.println("That number is too large!");
                }
                else if (decimal <= 65535) {
                   [COLOR="Red"] break;[/COLOR] // break to return valid input or Termination Request
                }
            }
            return decimal;
        }

    It seems to me, though, that the break is inside a loop! Any thoughts?

    Thank you.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You have misplaced something. I am being intentionally vague to make you think.

  3. #3
    louist is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Aha! I spotted that unneeded semicolon.
    I'm still at the point where I can't tell if I'm misusing keywords or if I'm making some sort of other syntax error. If the compiler doesn't spot it (as was the case with the extra semicolon) I get a little overwhelmed.

    Thank you for letting me know it was something I had done wrong, and pushing me to look harder.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your welcome, Im glad you solved it, please mark your thread solved in the thread tools.

Similar Threads

  1. break statement
    By AbdulAziz Bader in forum New To Java
    Replies: 17
    Last Post: 06-02-2010, 03:42 PM
  2. Break Recursion
    By AMaineJR in forum New To Java
    Replies: 6
    Last Post: 05-14-2010, 04:47 AM
  3. Break to certain class
    By Arez in forum New To Java
    Replies: 4
    Last Post: 10-20-2008, 04:13 AM
  4. How to use Break with a label
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:45 PM
  5. How to use Break
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:45 PM

Posting Permissions

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