Results 1 to 2 of 2
  1. #1
    cloverrea is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Angry loopinggggggggggg

    Hello can someone please tell me why my compiler is saying that the break is outside of the loop?

    public static String editItemNumber()
    {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter Item Number: ");
    String item = input.nextLine();
    int itemlength= item.length();
    do
    {
    }
    while (itemlength!=5);
    {
    System.out.print("**ERROR1** - Item Number must be five characters ");

    if(itemlength!= 5)break;
    }
    }
    }

  2. #2
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default Re: loopinggggggggggg

    Why is this needed?
    Java Code:
    if(itemlength!= 5)break
    When this loop continues when itemlength!=5:
    Java Code:
    do
    {
    }
    while (itemlength!=5);
    Try something like this instead:

    Java Code:
    public static String editItemNumber()
    {
    Scanner input = new Scanner(System.in);
    String item;
    int itemlength;
    System.out.println("Enter Item Number: ");
    do
    {
    item = input.nextLine();
    itemlength= item.length();
    if (itemlength!=5) System.out.print("**ERROR1** - Item Number must be five characters ");
    }
    while (itemlength!=5);
    }
    }
    Do you want it to repeat until the string input is 5 characters long? If so i believe the above code should do the trick.

    Hope this helps,

    Regards Serb.
    Last edited by SerbianSergeant; 10-13-2011 at 01:10 AM.

Tags for this Thread

Posting Permissions

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