1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
rosijanka [135]
3 years ago
5

rite a program that inserts parentheses, a space, and a dash into a string of 10 user-entered numbers to format it as a phone nu

mber. For example, 5153458912 becomes (515) 345-8912. If the user does not enter exactly 10 digits, display an error message. Continue to accept user input until the user enters 999. Save the file as PhoneNumberFormat.java.
Computers and Technology
2 answers:
Akimi4 [234]3 years ago
6 0

Answer and Explanation:

import java.util.Scanner;

public class PhoneNumberFormat {

  public static void main(String args[]) {

      Scanner scanner = new Scanner(System.in);

      do {

          System.out.print("\nEnter the Phone Number :");

          String textPhone = scanner.nextLine();

          if (textPhone.contains("999"))

              break;

          if (textPhone.matches("[0-9]+") && textPhone.length() == 10) {

              System.out.println("phoneNumber : " + "("

                      + textPhone.substring(0, 3) + ") "

                      + textPhone.substring(3, 6) + "-"

                      + textPhone.substring(6, 10));

          } else {

              System.out.println("Error message !");

          }

      } while (true);

      scanner.close();

  }

}

Agata [3.3K]3 years ago
5 0

Answer:

import java.util.Scanner;

public class PhoneNumber

{

public static void main(String[] args) {

   

    Scanner input = new Scanner(System.in);

       

       while(true) {

           

           System.out.print("Enter the phone number :");

           String phoneNumber = input.nextLine();

           

           if(phoneNumber.contains("999"))

               break;

           if(phoneNumber.length() == 10){

               phoneNumber = phoneNumber.replaceFirst("(\\d{3})(\\d{3})(\\d+)", "($1) $2-$3");

               System.out.printf("The formatted phone number is: %s \n", phoneNumber);

           }

           else

               System.out.println("The phone number must be exactly 10 digits!");

       }

       

}

}

Explanation:

- Unless we specify a breaking condition inside the <u>while loop</u>, it will iterate

- Ask the user for the phone number to be formatted

- Check if the phone number is 999, if yes, stop the loop

- If the phone number contains exactly 10 digits, format the phone number as requested using regex

- Print the formatted phone number

- If it has not 10 digits, print an error message

You might be interested in
Which of the four control methods is best suited for transfer of large blocks of data?
mihalych1998 [28]

Answer:

3

Explanation:

3 0
3 years ago
Which of the following is NOT a strength of monetary policy?
ipn [44]

your answer is b to the question


6 0
4 years ago
What are your strongest competencies? What will you do them? <br>❌NONSENSE ANSWER<br>​
Anna [14]

Answer:

1. Teamwork

2. Responsibility

3. Commercial Awareness

4. Decision Making

5. Communication

6. Leadership

7. Trustworthiness & Ethics

8. Results Orientation

9. Problem Solving

10. Organisational skills

8 0
3 years ago
Create an Entity-Relationship Diagram with the following requirements.
melamori03 [73]

Answer:

idk

Explanation:

6 0
3 years ago
Array elements must be ________ before a binary search can be performed.
Sladkaya [172]
In order.
You have to be able to see if its on the left or right side of the midpoint
7 0
3 years ago
Other questions:
  • How do you change brightness on acer laptop?
    7·1 answer
  • For which task is the WordArt gallery most useful?
    15·2 answers
  • How do you add a PDF assignment to google docs and be able to edit it?
    13·1 answer
  • Brainly wont show me the "I'm Done" button after an interactive ad, and I cant figure out why. I've watched the entire ad, click
    11·2 answers
  • Hey guys im just curious.... whats ur favorite number
    6·2 answers
  • Worksheet Identify the devices for moving the cursor around your computer screen
    12·1 answer
  • WILL GIVE BRAINLIEST!!!!!!!
    10·2 answers
  • what is example of application of machine learning that can be imposed in eduction. (except brainly.)
    9·1 answer
  • A construction-based client would like to develop an application that can analyze an image of machinery and overlay information
    8·1 answer
  • What is one way a pivottable could combine the following data?
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!