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 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...
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 JAVA program to display your complete names, your matric number and your course of...
Write a JAVA program to display your complete names, your matric number and your course of study, using the two access modifiers stated in (a) and (b) (a) Use static access modifier for the method declaration of the program class, also use class name Eee532MakeUp. Use any method name of your choice. (b) Use public access modifier for the method declaration of the program class, also use class name EceCourseJava. (2) Which of the programs in (a) or (b) is...
In Java, write a program that finds the first character to occur 3 times in a...
In Java, write a program that finds the first character to occur 3 times in a given string? EX. Find the character that repeats 3 times in "COOOMMPUTERRRR"
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT