Question

In: Computer Science

Write a java program to accept a telephone number with any number of letters. The output should display a hyphen after first 3 digits and subsequently a hyphen (-) after very four digits.

Write a java program to accept a telephone number with any number of letters. The output should display a hyphen after first 3 digits and subsequently a hyphen (-) after very four digits. Also, modify the program to process as many telephone numbers as the user wants

code optimization: no unnecessary variables , if statements and counters

sample output screen shown with 3 cases(atleast one with blank spaces ect.)Descriptive block and inline comments include.


Solutions

Expert Solution

For single phone no.:

import java.util.*;
public class Telephone {
    public static void main(String[] args) {
        String phoneNo; //To read the telephone no.
        //mark stores the mark where a hyphen will be placed
        //count is a counter that counts the no. of characters passed
   int i, mark = 3, count = 0;
        Scanner sc = new Scanner(System.in);
   System.out.print("Enter a telephone number: ");
   phoneNo = sc.nextLine();
        System.out.print("Telephone no. with hyphens: ");
   for(i = 0; i < phoneNo.length(); i++)
   {
            if(Character.isDigit(phoneNo.charAt(i))) //if character is a digit
            {
       System.out.print(phoneNo.charAt(i)); //print no.
                count++; //increase count by 1
                if(count == mark && i != phoneNo.length() - 1){
                    //if count reaches mark and the digit is not the last
                    System.out.print("-");
                    count = 0; //reset count
                    if(mark == 3) //after first three digits, mark changes to 4
                        mark = 4;
                }
            }
   }
        System.out.println();
    }
}

Output:

To modify the program for more than one telephone numbers, we just have to insert the same task in a loop. The code has been given below:

For more than one number:

import java.util.*;
public class Telephone {
    public static void main(String[] args) {
        String phoneNo; //To read the telephone no.
        //mark stores the mark where a hyphen will be placed
        //count is a counter that counts the no. of characters passed
   int i, mark, count, n;
        Scanner sc = new Scanner(System.in);
   System.out.print("How many nos. you want to enter: ");
        n = Integer.parseInt(sc.nextLine());
        while(n > 0){ //Iterating the same task for n times
            System.out.print("Enter a telephone number: ");
            phoneNo = sc.nextLine();
            System.out.print("Telephone no. with hyphens: ");
            count = 0;
            mark = 3;
            for(i = 0; i < phoneNo.length(); i++)
            {
                if(Character.isDigit(phoneNo.charAt(i))) //if character is a digit
                {
                    System.out.print(phoneNo.charAt(i)); //print no.
                    count++; //increase count by 1
                    if(count == mark && i != phoneNo.length() - 1){
                        //if count reaches mark and the digit is not the last
                        System.out.print("-");
                        count = 0; //reset count
                        if(mark == 3) //after first three digits, mark changes to 4
                            mark = 4;
                    }
                }
            }
            System.out.println();
            n--; //Decrementing n
        }
    }
}

Output:


Related Solutions

Modify Java program to accept a telephone number with any numberof the letters, using the...
Modify Java program to accept a telephone number with any number of the letters, using the if else loop. The output should display a hyphen after the first 3 digits and subsequently a hyphen (-) after every four digits. Also, modify the program to process as many telephone numbers as the user wants.
When all of the entries are valid, display an account number that shows first 3 digits of the telephone number and first two letters of the last name.
When all of the entries are valid, display an account number that shows first 3 digits of the telephone number and first two letters of the last name. using name John Smith phone number 1234567893 Using net beans take the first two letters from smith and first 3 numbers from the phone number and combine them to one line like this sm123.
Write a program that accept an integer input from the user and display the least number...
Write a program that accept an integer input from the user and display the least number of combinations of 500s, 100s, 50s, 20s, 10s, 5s, and 1s. Test your solution using this samples] a. Input: 250 Output: 1x200s, 1x50s b. Input: 1127 Output: 5x200s, 1x100s, 1x20s, 1x5s, 2x1s c. Input: 1127 Output: 5x200s, 1x100s, 1x20s, 1x5s, 2x1s d. Input: 19 Output: 1x10s, 1x5s, 4x1s ​[Hints] o Use division to determine the number of occurrence of each element (i.e. 200, 100)...
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
Java Write a program that will only accept input from a file provided as the first...
Java Write a program that will only accept input from a file provided as the first command line argument. If no file is given or the file cannot be opened, simply print “Error opening file!” and stop executing. A valid input file should contain two lines. The first line is any valid string, and the second line should contain a single integer. The program should then print the single character from the string provided as the first line of input...
Write a java program that will first display the following menu: Choose one of the following...
Write a java program that will first display the following menu: Choose one of the following 1- To add 2 double integers 2- To add 2 integer numbers 3- To add 3 double numbers 4- To add 3 integer numbers After reading user’s choice, use a switch case statement to call the corresponding method Void add 1 (double n1, double n2) Void add2() Double add3 (double n1, double n2, double n3) Double add4 ()
Write a Java program that will display different messages depending on your age. Your program should...
Write a Java program that will display different messages depending on your age. Your program should ask the user for his/her name and their age in years and give one or more answers from the following ones below: if the age of the user is less than 16, the program should print on the screen “You are not allowed to drive at the moment”. if the age of the user is less than 18, the program should print on the...
3. Write a Java program that loads a gallery.xml file, determines the number of photos (should...
3. Write a Java program that loads a gallery.xml file, determines the number of photos (should be only 3) in it and prints out the number of photos in the gallery.
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
in .java Write a program that reads an integer with 3 digits and prints each digit...
in .java Write a program that reads an integer with 3 digits and prints each digit per line in reverse order. Hint: consider what you get from these operations: 319%10, 319/10, 31%10, ... Enter an integer of exactly 3 digits(e.g. 538): 319 9 1 3 Hint: consider what you get from these operations: 319%10 319/10 31%10
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT