2

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

    }
}
4
  • Have you debugged your code? Looks to me that entering 999 will mean the code never exits the while loop. Commented Nov 18, 2015 at 19:37
  • 1
    There are potential NPE if you just left the dialog empty. Commented Nov 18, 2015 at 19:42
  • What I understand is that in first statement you are asking for 999 to quit. and then in next code you are matching if the user hasnot enter the >10 digits. You have to check also that the if user has enter 3 digits and that 3 digits is 999 then exit the loop. I believe this is what is going to work Commented Nov 18, 2015 at 19:43
  • @StackFlowed agree about potential NPE but, the NPE is only thrown if you press Cancel or Close the input dialogue box. If you press OK without providing an input, it won't be null but a string of length 0 (empty string). Commented Nov 18, 2015 at 20:03

4 Answers 4

5

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

}
Sign up to request clarification or add additional context in comments.

4 Comments

He wants to exist the loop and it does? Is there some other problem too?
@brso05 it won't because he forces the input to be 10 characters long.
@mibac what if he puts the if before the while loop that forces the 10 digits?
@mibac according to his question/specifications this is actually the more correct answer...
4

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

5 Comments

@azurefrog He doesn't modify the PhoneNumber variable itself actually
@JamesB it's fine to change the do while did you try the code?
@JamesB this is a correct answer 1 of many. You shouldn't downvote correct answers. Try the code and see if it works...
@JamesB that cheers was for the compliment, messed up the reference, the comments were coming so fast maybe that's why.
@Raf no problems, nice to give credit where it is due.
2

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.

Comments

2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.