BDoe
BDoe

Reputation: 21

How to exit from a do while loop in Java?

simple program to output number into phone number format I cant seem to exit the loop I'm not sure what I'm doing wrong I thought !PhoneNumber.equals("999"); would exit the loop when the user inputs 999 but it isn't working. Can anyone help me here's my code

import javax.swing.*;

public class PhoneNumberFormat 
{

    public static void main(String[] args) 
    {
      String PhoneNumber;
      int numLength= 10;

      do
      {

          PhoneNumber = JOptionPane.showInputDialog(null, 
                 "Enter your 10 digit phone number or enter 999 to quit");
          while(PhoneNumber.length() != numLength)
          {
              PhoneNumber = JOptionPane.showInputDialog(null,
                  "Error: You Entered " + PhoneNumber.length() + " digits\nPlease"
                      + " Enter a 10 digit Phone number");    
          }

            StringBuilder str = new StringBuilder (PhoneNumber);
            str.insert(0, '(');
            str.insert(4, ')');
            str.insert(5,' ');
            str.insert(9, '-');

        JOptionPane.showMessageDialog(null, "Your telephone number is " +str.toString());

      }while(!PhoneNumber.equals("999"));

    }
}

Upvotes: 2

Views: 3383

Answers (4)

Raf
Raf

Reputation: 7649

If you wish to exist when the 999 you need to add an if condition to watch for it.

public static void main(String[] args) {
    String PhoneNumber;
    int numLength = 10;

    do {
        PhoneNumber = JOptionPane.showInputDialog(null,
                "Enter your 10 digit phone number or enter 999 to quit");

        // add this condition to exit the loop, as well protect against NPE
        if (PhoneNumber == null || PhoneNumber.equals("999")) {
            break;
        }

        while (PhoneNumber.length() != numLength) {
            PhoneNumber = JOptionPane.showInputDialog(null,
                    "Error: You Entered " + PhoneNumber.length()
                            + " digits\nPlease"
                            + " Enter a 10 digit Phone number");

            //protect against NPE
            if(PhoneNumber == null) 
               PhoneNumber = "";
        }

        StringBuilder str = new StringBuilder(PhoneNumber);
        str.insert(0, '(');
        str.insert(4, ')');
        str.insert(5, ' ');
        str.insert(9, '-');

        JOptionPane.showMessageDialog(null, "Your telephone number is "
                + str.toString());

    } while (!PhoneNumber.equals("999"));

}

Upvotes: 5

StackFlowed
StackFlowed

Reputation: 6816

Code with fixing potential NPE and solving your problem should look like:

import javax.swing.*;

public class PhoneNumberFormat 
{

    public static void main(String[] args) 
    {
      String PhoneNumber;
      int numLength= 10;

      do
      {

          PhoneNumber = JOptionPane.showInputDialog(null, 
                 "Enter your 10 digit phone number or enter 999 to quit");
          while(PhoneNumber!=null && PhoneNumber.length() != numLength)
          {
              PhoneNumber = JOptionPane.showInputDialog(null,
                  "Error: You Entered " + PhoneNumber.length() + " digits\nPlease"
                      + " Enter a 10 digit Phone number");    
          }

            StringBuilder str = new StringBuilder (PhoneNumber);
            str.insert(0, '(');
            str.insert(4, ')');
            str.insert(5,' ');
            str.insert(9, '-');

        JOptionPane.showMessageDialog(null, "Your telephone number is " +str.toString());

      } while(!PhoneNumber.substring(0,3).equals("999"));

    }
}

Fixes are on lines

while(PhoneNumber!=null && PhoneNumber.length() != numLength)

and

while(!PhoneNumber.substring(0,3).equals("999"));

Upvotes: 2

Sauhard Gupta
Sauhard Gupta

Reputation: 219

If you want to use 999 as an input option to exit, allow the user to enter 999 as a valid input in the first place.

  int numLength= 10;
  do
  {

      PhoneNumber = JOptionPane.showInputDialog(null, 
             "Enter your 10 digit phone number or enter 999 to quit");
      while(PhoneNumber.length() != numLength)
      {
          PhoneNumber = JOptionPane.showInputDialog(null,
              "Error: You Entered " + PhoneNumber.length() + " digits\nPlease"
                  + " Enter a 10 digit Phone number");    
      }

Here, you refuse to consider the input if its length is anything but 10.

Upvotes: 2

martinez314
martinez314

Reputation: 12332

You are forcing the number to be 10 digits, so what do you expect? It will never be equal to the three-digit "999".

Maybe you meant to do:

while(!PhoneNumber.startsWith("999"));

Upvotes: 4

Related Questions