Question

In: Computer Science

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.

Solutions

Expert Solution

Please follow the code and comments for description :

CODE :

import java.util.HashMap; // required imports for the code to run

import java.util.Map;
import java.util.Scanner;

public class Telephone { // calss to run the methods

public static void main(String[] args) { // driver method
  
Scanner sc = new Scanner(System.in); // scanner class to get the data from the user

System.out.print("Enter the Telephone letters : "); // prompt the user to enter the data
String input = sc.nextLine(); // get the data
String lowerInput = input.toLowerCase(); // converting the data to lowercase

TeleNumProcessor NumProcessor = new TeleNumProcessor(lowerInput); // creating an object ofr the class and pass the data
String result = NumProcessor.process(); // call the method and save the result

System.out.println("---------------------------------------------------"); // print the data to console
System.out.println("The Entered Telephone letters are : " + input);
System.out.println("The Phone number is : " + result);
System.out.println("---------------------------------------------------");
}

}

class TeleNumProcessor { // class that gets the numbers for the input

Map match = new HashMap<>(); // map that stores the matching numbers
String strToProcess; // required initialisations

public TeleNumProcessor(String strToProcess) { // method that takes the input and returns the number string
this.strToProcess = strToProcess;
match.put("[a-c]", "2"); // saving the data to the map for the patterns
match.put("[d-f]", "3");
match.put("[g-i]", "4");
match.put("[j-l]", "5");
match.put("[m-o]", "6");
match.put("[p-s]", "7");
match.put("[t-v]", "8");
match.put("[w-z]", "9");
}

public String process() { // method to process the input data

if (!"2".equals(strToProcess)) { // checking if the input is 2 or not
match.forEach((regex, replacement) -> { // method to check for the regex pattern matcher
strToProcess = strToProcess.replaceAll(regex, replacement); // replace the dat and place it to the string
});
}
return strToProcess.substring(0, 3) + "-" + strToProcess.substring(3, 7); // return the formatted data
}
}

OUTPUT :

Enter the Telephone letters : fewfwef
The Entered Telephone letters are : fewfwef
The Phone number is : 339-3933


Related Solutions

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 wantscode optimization: no unnecessary variables , if statements and counterssample output screen shown with 3 cases(atleast one with blank spaces ect.)Descriptive block and inline comments include.
in java code Modify your program as follows: Ask the user for the number of hours...
in java code Modify your program as follows: Ask the user for the number of hours worked in a week and the number of dependents as input, and then print out the same information as in the initial payroll assignment. Perform input validation to make sure the numbers entered by the user are reasonable (non-negative, not unusually large, etc). Let the calculations repeat for several employees until the user wishes to quit the program. Remember: Use variables or named constants...
Create a C++ program that will accept any number of grades for an exam. The grades...
Create a C++ program that will accept any number of grades for an exam. The grades will be input as 4 for an A, 3 for a B, 2 for a C, 1 for a D, and 0 for an F. After all grades have been entered, allow the user to enter -1 to exit. Output the number of grades in each category. Using arrays.
Java program to print all the powers of 2 below a certain number. Calculate sum, accept...
Java program to print all the powers of 2 below a certain number. Calculate sum, accept upper limit and make sure sum variable is long type.   Run- Enter the upper limit: 100 5 + 8 + 9 + 11 + 20 + 32 + 30 = 115
To make telephone numbers easier to remember, some companies use letters to show their telephone number....
To make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, using letters, the telephone number 438-5626 can be shown as GET LOAN. In some cases, to make a telephone number meaningful, companies might use more than seven letters. For example, 225-5466 can be displayed as CALL HOME, which uses eight letters. Instructions Write a program that prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone...
: Create a Java program that will accept a regular expression and a filename for a...
: Create a Java program that will accept a regular expression and a filename for a text file. The program will process the file, looking at every line to find matches for the regular expression and display them. Regular Expression Format : The following operators need to be accepted: + - one or more of the following character (no groups) * - zero or more of the following character (no groups) [] – no negation, no character spans – the...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of miles, and the class of journey (1,2, or 3, for first, second, and third class respectively), for a train journey. The program should then calculate and display the fare of journey based on the following criteria: Note: Use Switch...case and if...else construct First (1) Class Second (1) Class Third (3) Class First 100 mile $ 3 per mile $ 2 per mile $ 1.50...
Tokenizing Telephone Numbers Write java application that inputs a telephone number as a string in the...
Tokenizing Telephone Numbers Write java application that inputs a telephone number as a string in the form (555) 555-5555. The application should use String method split to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be...
(Tokenizing Telephone Numbers) Write a program that inputs a telephone number as a string in the...
(Tokenizing Telephone Numbers) Write a program that inputs a telephone number as a string in the form (555) 555-5555. The program should use function strtok to extract the area code as a token, the first three digits of the telephone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. The program should convert the area-code string to int and convert...
Develop a rudimentary Java program that will allow anyone to enter any number of grades and...
Develop a rudimentary Java program that will allow anyone to enter any number of grades and then calculate grade point average. For reference, a grade point average is a weighted average of a set of grades based on the relative number of credits. For example, a 1 credit “A” (4.0) should count twice as much as a .5 credit “C” (3.0), and a 1.5 credit “A+”(4.33) should count three times as much as the “C” and 1.5 times as much...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT