***Need the Full Java Code!*** What your program should do: *Prompt the user to
ID: 3771791 • Letter: #
Question
***Need the Full Java Code!***
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.
*Further Details:*
The dictionary file should be in the same directory as your executable. You should use the files on unixlab ~whsu/csc210/Projects/P10/words6752 or ~whsu/csc210/Projects/P10/words10683 as your dictionary files. (There’s also a very short file, ~whsu/csc210/Projects/P10/words5, for initial testing.) All these files are also accessible through your browser at
http://userwww.sfsu.edu/~whsu/csc210/Projects/P10
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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;
public class String3or4Test {
public static void main(String[] theArgs) {
System.out.println("Enter file name: ");
Scanner sc = new Scanner(System.in);
String inFileName = sc.nextLine();
try {
Scanner input = new Scanner(new File(inFileName));
ArrayList<String> w3 = new ArrayList<String>();
ArrayList<String> w4 = new ArrayList<String>();
String word = "";
while(input != null && input.hasNext()) {
word = input.next();
if(word != null) {
if(word.length() == 3)
w3.add(word);
if(word.length() == 4)
w4.add(word);
}
}
String3or4Test.writeToFile("D:/ravi/Cheg/short3.txt", w3);
String3or4Test.writeToFile("D:/ravi/Cheg/short4.txt", w4);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void writeToFile(String filename, ArrayList<String> data) throws FileNotFoundException {
PrintStream output = new PrintStream(new File(filename));
if(data != null) {
for(String str: data)
output.println(str);
}
}
}
--output--
D: aviCheg>java String3or4Test
Enter file name:
D:/ravi/Cheg/word3or4.txt
curd
daft
java
name
curd
daft
more
used
test
line
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.