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

Please include the text file, thanks What your program should do: Prompt the use

ID: 3770567 • Letter: P

Question

Please include the text file, thanks

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.

You will need to know how to read and write a text file; see Chapter 9 Slides 36-46 for details. (Hint: this is very similar to the exercise on Slide 46.) 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.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;

/**
* @author Srinivas Palli
*
*/
public class ProcFile {

   /**
   * @param args
   */
   public static void main(String[] args) {

       Scanner scanner = null;

       String w3[] = new String[100];
       String w4[] = new String[100];
       int w3Count = 0, w4Count = 0;
       try {

           scanner = new Scanner(System.in);
           // prompt user for dictionary file
           System.out.print("Enter the file name :");
           String fileName = scanner.next();

           scanner = new Scanner(new File(fileName + ".txt"));
           while (scanner.hasNext()) {
               String line = scanner.next();
               if (line.length() == 3) {
                   w3[w3Count++] = line;
               } else if (line.length() == 4) {
                   w4[w4Count++] = line;

               }
           }

           writeData(w3, "shorts3", w3Count);
           writeData(w4, "shorts4", w4Count);

       } catch (Exception e) {
           // TODO: handle exception
       } finally {

           scanner.close();
       }
   }

   /**
   * method to write data words with given words, fileName and number of words
   * in the words array
   *
   * @param words
   * @param fileName
   * @param noOfWords
   */
   public static void writeData(String words[], String fileName, int noOfWords) {

       try {

           File file = new File(fileName + ".txt");

           // if file doesnt exists, then create it
           if (!file.exists()) {
               file.createNewFile();
           }

           FileWriter fw = new FileWriter(file.getAbsoluteFile());
           BufferedWriter bw = new BufferedWriter(fw);
           for (int i = 0; i < noOfWords; i++) {

               bw.write(words[i] + " ");
           }
           bw.close();

       } catch (Exception e) {
           // TODO: handle exception
       }

   }
}

Input file: words5.txt

cat
intolerable
curd
daft
dog

OUTPUT:

Enter the file name :words5

OUTPUT FILES:

shorts3.txt

cat
dog

shorts4.txt

curd
daft