Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab 21 Text File Output for Phone Number Words Description: Standard telephone k

ID: 3688723 • Letter: L

Question

Lab 21

Text File Output for Phone Number Words

Description:

Standard telephone keypads containe the digits zero through nine. The numbers two through nine each have three letters 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".

Each seven-letter word or word combination corresponds to exactly one seven-digit telephone number, but a seven-digit number corresponds to many seven-letter words, most of which are not English words. It is possible, for example, that the owner of a barbershop would be pleased to know that the shop's telephone number 424-7288 corresponds to "HAIRCUT", or a liquor store's number 233-7226 corresponds to "BEERCAN".

Objective:

Write a program that asks a user to enter a seven-digit phone number and then writes to the file 'numbers.txt' every possible seven-letter word combination corresponding to that number. There are 2187 (i.e. 37) such combinations. Treat the digits 0 & 1 as spaces, and the rest as shown:

Finally:

Submit both PhoneNumbers.java and an example numbers.txt to the dropbox

Remember that your source code MUST have appropriate headers and MUST contain appropriate comments.

Explanation / Answer

package assignment;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class PhoneNumbers {
   public static void main(String []args) {

       char codes[][]= {{' '},{' '},
                         {'A','B','C'},
                         {'D','E','F'},
                         {'G','H','I'},
                         {'J','K','L'},
                         {'M','N','O'},
                         {'P','R','S'},
                         {'T','U','V'},
                         {'W','X','Y'}
                         };
       Scanner scan = new Scanner(System.in);
       ArrayList<String> alphaNums = new ArrayList<String>();
       ArrayList<String> tempAL = new ArrayList<String>();
       System.out.println("Enter seven digit number:");
       int num = scan.nextInt();
      
       int rem = 0;
       int temp = num;
       String numstr = Integer.toString(num);
       String tempStr = null;
       //alphaNums.add(numstr);
       while(temp > 0) {
           rem = temp % 10;
           temp = temp/ 10;
           if(alphaNums.size() == 0) {
               for(char ch: codes[rem]) {
                   alphaNums.add(numstr.replaceFirst(Integer.toString(rem), Character.toString(ch)));
                  
               }
               //System.out.println("===>"+alphaNums.toString());
           } else {
               tempAL = new ArrayList<String>();
               for(int i = 0; i < alphaNums.size(); i++) {
                   tempStr = alphaNums.get(i);
                   //alphaNums.set(i, tempStr.replace(Integer.toString(rem).charAt(0), codes[rem][0]));
               //   System.out.println("=>"+alphaNums.get(i));
                   for(int j = 0; j < codes[rem].length; j++)
                       tempAL.add(tempStr.replaceFirst(Integer.toString(rem), Character.toString(codes[rem][j])));
                  
               }
               alphaNums.clear();
               alphaNums.addAll(tempAL);
           }
  
       }
       //System.out.println(alphaNums.size()+" "+alphaNums.toString());
       try {
           writeToFile("D:/ravi/Cheg/numbers.txt",alphaNums);
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
//       for(int i = 0; i < alphaNums.size(); i++) {
//           System.out.println(alphaNums.get(i));
//       }
   }
  
   public static void writeToFile(String fileName, ArrayList<String> alphaNums) throws IOException {
       File file = new File(fileName);
          // creates the file
          file.createNewFile();
          // creates a FileWriter Object
          FileWriter writer = new FileWriter(file);
          // Writes the content to the file
          for(int i = 0; i < alphaNums.size(); i++) {
              writer.write(alphaNums.get(i));
              writer.write(" ");
              writer.flush();
          }
          writer.close();
   }
}