To make telephone numbers easier to remember, we usually map each digit to a let
ID: 3769922 • Letter: T
Question
To make telephone numbers easier to remember, we usually map each digit to a letter of the alphabet, with a choice of 3 or 4 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
For this project, we’ll make these simplifying restrictions when we map a 7-digit telephone number to words: the first word always has three letters, the second word always has four letters, and there are no 0’s or 1’s in the telephone number.
Obviously, the “words” are only interesting if they are real words, i.e., they can be found in a dictionary. For this project, your code will read a dictionary file, and match its entries with a telephone number. You will need to set up String arrays to store words from the dictionary file.
What your program should do:
Prompt the user to enter the name of the dictionary file to use (a text file)
Set up two arrays of Strings, w3[] and w4[]
Read all words from the file
store all 3-letter words from the file into w3[]
store all 4-letter words from the file into w4[]
Write w3[] into a file called shorts3
Write w4[] into a file called shorts4
You must create and fill the arrays w3[] and w4[] correctly; otherwise points will be deducted.
The dictionary file should be in the same directory as your executable. Use this file as your dictionary file : http://userwww.sfsu.edu/whsu/csc210/Projects/P10/words6752
A sample run:
unixlab% ls
ProcFile.class words10683 words5 words6752
unixlab% cat words5
cat
intolerable
curd
daft
dog
unixlab% java ProcFile
Enter name of dictionary file: words5
unixlab% ls
procFile shorts3 shorts4 words10683 words5 words6752
unixlab% cat shorts3
cat
dog
unixlab% cat shorts4
curd
daft
unixlab%
In more detail, suppose words5 is used to test ProcFile. ProcFile starts reading words5, line by line.
The first word read is cat. This is stored in w3[0].
The next word read is intolerable. This is not stored.
The next word read is dog. This is stored in w3[1].
The next word read is curd. This is stored in w4[0]. Finally, daft is read and stored in w4[1].
The contents of w3[] are written to the file shorts3. The contents of w4[] are written to the file shorts4.
Explanation / Answer
PFB the Java Code with comments
import java.util.*;
import java.lang.*;
import java.io.*;
package chegg
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the name of Dictionary File");
String fileName = in.next(); /* Store the filename */
in.close(); /* Close the Scanner handle */
File file = new File(fileName); /* Create file object, which the Scanner will use */
try {
Scanner sc = new Scanner(file);
ArrayList<String> w3 = new ArrayList<String>();
ArrayList<String> w4 = new ArrayList<String>();
while (sc.hasNextLine()) {
String word = sc.next();
switch(word.length())
{
case 3 : w3.add(word);
case 4 : w4.add(word);
default : break;
}
}
sc.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.