Question

String phoneNumber = JOptionPane . showinputDialog ( What is your phone number?); while(phoneNumber.length) !- 10) JOptionPane.showMessageDialog(null, phoneNumber + is invalid, please enter a valid 10 digit n String phoneNumberJOptionPane.showInputDialog(What is your phone number?); JOptionPane.showMessageDialog(null, nameIn +jerseyNumber +In + phoneNumber);How do i allow the format xxx-xxx-xxxx?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.regex.Pattern;

import javax.swing.JOptionPane;

public class Test{

public static void main(String args[]) {

while(true){

String phoneNumber = JOptionPane.showInputDialog("What is your phone number ?");

if((Pattern.matches("[0-9]{3}-[0-9]{3}-[0-9]{4}", phoneNumber)))

break;

else

JOptionPane.showMessageDialog(null, phoneNumber + " is invalid," + " please enter a valid 10 digit number");

}

}

}

Here i used regex to allow the format xxx-xxx-xxxx. Please check the bold statement in the code.

Let me know if you have any queries or clarifications.... :)

Add a comment
Know the answer?
Add Answer to:
How do i allow the format xxx-xxx-xxxx? String phoneNumber = JOptionPane . showinputDialog ( "What is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • build a phone number from digits entered by your user. Your program will loop until 10...

    build a phone number from digits entered by your user. Your program will loop until 10 valid digits have been entered. It will then use those digits to display the phone number entered using the format: XXXXXXXXXX (or (XXX) XXX – XXXX for extra credit). The program will ask for a digit, it will check to see if the digit is actually between 0 and 9 inclusively. If so, the digit will be stored as the next number in the...

  • In this programming assignment we will review string and character manipulations. You will be developing a...

    In this programming assignment we will review string and character manipulations. You will be developing a Java program that will prompt users for person’s last name, year of birth, and employee number in a predefined specific format. You will then parse the input string and print it in separate lines along with validations. The input format will be of the form :,< employee number > You will need to separate three segments in order to print it as: Last name...

  • i nedd to know how to fix it import javax.swing.JOptionPane: public class Conv void length (double...

    i nedd to know how to fix it import javax.swing.JOptionPane: public class Conv void length (double d) JOptionPane.showMessageDialog(null, "the equvialent cm 13 "+(double) /100) double length() double d-Double. parseDouble (JOptionPane. ShowMessageDialog(null,"enter the meter value return d*100.0: void distance (double d) JOptionPane. showMessageDialog(null,"the equvialent meter is : "+(double) /1000); double distance() double d-Double. parseDouble (JoptionPane. showMessageDialog(null, enter the kilometna return a 1000; SLOVO publie main(String args) Conv c-new Conv(): boolean b=true; while(b) { int a Integer.parseInt(JOption Pane.showMessageDialog(null, "Enterin l.to convert to...

  • Hello I need help with this. I am trying to use JOptionpane to ask 5 questions....

    Hello I need help with this. I am trying to use JOptionpane to ask 5 questions. However I cannot get the program to do anything after an initial selection is made. Here is the assignment. Write an application that creates a quiz, which contains at least five questions about a hobby, popular music, astronomy, or any other personal interest. Each question should be a multiple choice question with at least three options. When a user answers a question correctly, display...

  • This is the source code: Please incorporate the Exception Handling import javax.swing.JOptionPane; import java.awt.*; public class...

    This is the source code: Please incorporate the Exception Handling import javax.swing.JOptionPane; import java.awt.*; public class GuessingGame { public static void main(String[] args) { int computerNumber = (int) (Math.random() * 10 + 1); System.out.println("The correct guess would be " + computerNumber); int userAnswer = 0; int count = 0; while (computerNumber != userAnswer) { count++; String response = JOptionPane.showInputDialog(null, "Enter a guess between 1 and 10"); userAnswer = Integer.parseInt(response); String result = null; if (userAnswer == computerNumber) { result =...

  • This is Python The program should accept input from the user as either 7-digit phone number...

    This is Python The program should accept input from the user as either 7-digit phone number or 10-digit. If the user enters 7 characters, the program should automatically add "512" to the beginning of the phone number as the default area code. Dash (hyphen) characters do not count in input character count, but must not be random. If the user enters dashes the total character count must not exceed 12. The program should not crash if the user enters invalid...

  • I need the following written in Java please, thank you ' We want you to implement...

    I need the following written in Java please, thank you ' We want you to implement a java class that will show details on users and throw exceptions where needed. The following requirements specify what fields you are expected to implement in your User class: - A private String firstName that specifies the first name of the user - A private String lastName that specifies the last name of the user - A private int age that specifies user's age...

  • Please help, Array does not print properly. I need to print out the 8 class numbers...

    Please help, Array does not print properly. I need to print out the 8 class numbers entered by the user ! Java only using J option pane, you cannot use ulti or anything else only J option pane Program compiles but does not print the array correctly! import javax.swing.JOptionPane; public class programtrial {    public static void main(String[] args) {    int newclass = 0;    int countclass = 0;    final int class_Max = 8;    int[] classarray =...

  • DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to...

    DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. The program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2. Add a new contact. 3. Search for a contact...

  • I need this script to SEARCH employee by SSN and EDIT employee information in the format...

    I need this script to SEARCH employee by SSN and EDIT employee information in the format provided. Now you will continue to employ the list data structure and utilize functions to add the following two new functions: Search employee by SSN: This functionality makes use of looping and string parsing to search all the employees in the list and returns the information of the employee who has a SSN similar to the one that the user provided. Your system should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT