Question

In: Computer Science

IN JAVA pls Standard telephone keypads contain the digits zero through nine. The numbers two through...

IN JAVA pls

Standard telephone keypads contain the digits zero through nine. The numbers two through
nine each have 3~4 letters (case insensitive) associated with them. Many people find it
difficult to memorize phone numbers, so they use the correspondence between digits and
letters to develop seven-letter words that correspond to their phone numbers. For example, a
person whose telephone number is 686-2377 might remember it as "NUMBERS."
Digit Letters
2 A B C
3 D E F
4 G H I
5 J K L
6 M N O
7 P Q R S
8 T U V
9 W X Y Z
2. Each seven-letter word corresponds to exactly one seven-digit telephone number, but a
seven-digit number corresponds to many seven-letter strings, most of which are not words.
3. In this project you will develop a program which will find all the corresponding English
words given any specific telephone number. Here are the detailed requirements:
a. create a static void test() method.
b. The test method will first ask the user to type in through keyboard a 7-digit telephone
number.
c. If the number typed in by the user is not 7 digits or the number contains some number
of the digit ‘0’ or ‘1’, then your program reports an error and asks to type in another
number.
d. If the number typed in by the user is a 7-digit telephone number made up of the digits
‘2’ through ‘9’, then your program will print out all the English words corresponding
to the telephone number (and the total number of them), using the given English
word library in the EnglishWordList.txt”.

EnglishWordList.txt contains the following words

abalone

abandon

abasers

abashes

ablates

caffein

Solutions

Expert Solution

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class StringsFromPhoneDigits {
   public static void test()
   {
       //for numbers adding the map corresponding key pad string for key pad numbers
       HashMap<Character,String> map = new HashMap<Character,String>();
map.put('1', "");
map.put('2',"abc");
map.put('3',"def");
map.put('4',"ghi");
map.put('5',"jkl");
map.put('6',"mno");
map.put('7',"pqrs");
map.put('8',"tuv");
map.put('9',"wxyz");
map.put('0', "");
  
//taking valid input from the user
       System.out.print("Enter a 7-digit telephone number :");
       Scanner input = new Scanner(System.in);
       String digits = input.next();
       boolean result = true;
       while(true)
       {
           if(digits.matches("^[2-9]{7}$")) {
               break;
           }
           else {
               System.out.println("an error occured with your input please enter another number");
               System.out.print("Enter a 7-digit telephone number :");
               digits = input.next();
           }
       }
      
       //processing input for getting possible words with matches input string
       ArrayList<String> res = new ArrayList<String>();
ArrayList<String> preres = new ArrayList<String>();
res.add("");
for(int i = 0; i < digits.length(); i++) {
String letters = map.get(digits.charAt(i));
if (letters.length() == 0)
continue;
for(String str : res) {
for(int j = 0; j < letters.length(); j++)
preres.add(str + letters.charAt(j));
}
res = preres;
preres = new ArrayList<String>();
}
  
   //taking data from the file
   ArrayList<String> stringsFromFile = new ArrayList<String>();
   System.out.println("\nThe Words From EnglishWordList.txt are\n--------------------------------");
   File file = new File("C:\\Users\\Mirchi\\Desktop\\chegg\\TextFiles\\EnglishWordList.txt");
   try {
   Scanner sc = new Scanner(file);
     
   while (sc.hasNextLine()) {
   String i = sc.nextLine();
   stringsFromFile.add(i);
   System.out.println(i);
   }
   sc.close();
   }
   catch (FileNotFoundException e) {
   e.printStackTrace();
   }
  
   //checking from the file weather that words are there are not
   int count = 0;
   for(String i:res) {
       if(stringsFromFile.contains(i)) {
           count++;
       }
   }
   if(count!=0) {
       System.out.println("\n\nTotal Count of words form with given phone digits are = "+count);
       System.out.println("\nThe Result words are\n----------------");
       for(String i:res) {
          if(stringsFromFile.contains(i)) {
              System.out.println(i);
          }
      }
   }
   //if strings are not there in the file printing all possible words
   else {
       System.out.println("\nThere is no match with result strings with the file");
       System.out.println("\n\nTotal Count of words form with given phone digits are = "+res.size());
      System.out.println("The Words are\n----------------");
       for(String i:res) {
              System.out.println(i);
      }
   }
   }

   public static void main(String[] args)
   {
       test();
   }
}


Related Solutions

In Java lang Standard telephone keypads contain the digits zero through nine. The numbers two through...
In Java lang Standard telephone keypads contain the digits zero through nine. The numbers two through nine each have 3~4 letters (case insensitive) associated with them. Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might remember it as "NUMBERS." Digit Letters 2 ABC 3 DEF 4 GHI 5 JKL 6 MNO...
Problem in chapter 15 of Java book. Need a solution please! Problem: Standard telephone keypads contain...
Problem in chapter 15 of Java book. Need a solution please! Problem: Standard telephone keypads contain the digits zero through nine. The numbers two through nine each have three letters associated with them (as seen below). Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might use this tool to develop the...
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...
When analyzing the last digits of telephone numbers in Sandtown, it was found that among 1500...
When analyzing the last digits of telephone numbers in Sandtown, it was found that among 1500 randomly selected phone numbers, 225 had zero as the last digit. If the digits are selected randomly, the proportion of zeroes should be 0.1 because from probability, 0 is one of the 10 possible digits. Use the p-value method with a 0.01 level of significance to test the claim that the proportion of zeroes is not equal to 0.1. Use the sample data to...
When analyzing the last digits of telephone numbers in Sandtown, it was found that among 1500...
When analyzing the last digits of telephone numbers in Sandtown, it was found that among 1500 randomly selected phone numbers, 225 had zero as the last digit. If the digits are selected randomly, the proportion of zeroes should be 0.1 because from probability, 0 is one of the 10 possible digits. 1. Use the p-value method with a 0.01 level of significance to test the claim that the proportion of zeroes is not equal to 0.1. 2. Use the sample...
When analyzing the last digits of telephone numbers in Sandtown, it was found that among 1500...
When analyzing the last digits of telephone numbers in Sandtown, it was found that among 1500 randomly selected phone numbers, 225 had zero as the last digit. If the digits are selected randomly, the proportion of zeroes should be 0.1 because from probability, 0 is one of the 10 possible digits. Use the p-value method with a 0.01 level of significance to test the claim that the proportion of zeroes is not equal to 0.1. Be sure to use the...
In PYTHON On a standard telephone, alphabetic letters are mapped to numbers in the following fashion:...
In PYTHON On a standard telephone, alphabetic letters are mapped to numbers in the following fashion: A, B, and C = 2 D, E, and F = 3 G, H, and I = 4 J, K, and L = 5 M, N, and O = 6 P, Q, and R = 7 T, U, and V = 8 W, X, Y, and Z = 9 Write a fruitful function that takes any string of alphanumeric characters and returns a string...
[jAVA] Assume the following array is defined and initialized (it will already contain numbers before your...
[jAVA] Assume the following array is defined and initialized (it will already contain numbers before your code runs): public static void main( String [ ] args ) { int [ ] numbers = . . . ; // An array of integers 1) Provide code that will print the entire contents of numbers in reverse order (from the end to the beginning) 2) Provide code that will print true if the numbers are in strictly ascending order (that is, if...
Using the digits 2 through 8, find the number of different 5-digit numbers such that: (a)...
Using the digits 2 through 8, find the number of different 5-digit numbers such that: (a) Digits can be used more than once. (b) Digits cannot be repeated, but can come in any order. (c) Digits cannot be repeated and must be written in increasing order. (d) Which of the above counting questions is a combination and which is a permutation? Explain why this makes sense
Using the digits 0 through 8, find the number of different 5-digit numbers such that: a....
Using the digits 0 through 8, find the number of different 5-digit numbers such that: a. Digits can be used more than once. b. Digits cannot be repeated, but can come in any order. c. Digits cannot be repeated and must be written in increasing order. d. Which of the above counting questions is a combination and which is a permutation? Explain why this makes sense.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT