*************** JAVA PROGRAM ONLY****************** To make telephone numbers ea
ID: 3772284 • Letter: #
Question
***************JAVA PROGRAM ONLY******************
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
Program:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
public class s {
public static void main(String[] args) {
int count=10;
String w3[]=new String[10000];
String w4[]=new String[10000];
int k=0,j=0;
BufferedWriter bufferedWriter1 = null;
BufferedWriter bufferedWriter2 = null;
try {
File myFile1 = new File("shorts3.txt"); // output file for 3-letter words
File myFile2 = new File("shorts4.txt"); // output file for 4-letter words
Writer writer1 = new FileWriter(myFile1);
bufferedWriter1 = new BufferedWriter(writer1);
Writer writer2 = new FileWriter(myFile2);
bufferedWriter2 = new BufferedWriter(writer2);
try (BufferedReader br = new BufferedReader(new FileReader("words5.txt")))// input file
{
String sCurrentLine;int i=0;
while ((sCurrentLine = br.readLine()) != null) {
String[] splited = sCurrentLine.split("\s+");
String s1=splited[0];
if(s1.length()==3)
{w3[k]=s1;
bufferedWriter1.write(w3[k]);
bufferedWriter1.write(" ");
k++;
}
else if(s1.length()==4)
{w4[j]=s1;
bufferedWriter2.write(w4[j]);
bufferedWriter2.write(" ");
j++;
}
i++;
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try{
if(bufferedWriter1 != null && bufferedWriter2 != null) {bufferedWriter1.close();bufferedWriter2.close();}
} catch(Exception ex){
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.