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

Could I get some help with this assignment In this assignment, you\'ll write a p

ID: 3828909 • Letter: C

Question

Could I get some help with this assignment

In this assignment, you'll write a program which reads a text file, and prints a histogram of its word sizes. So, for example, the historgram should show the number of words of length 1, of length 2, etc., up to the number of words of length n, where n is some constant defined in your program.

To obtain text files that are suitably long, you could try Project Gutenberg, a site containing thousands of free books available in several different formats (for this assignment, you're interested in files in plain text format).

For your convenience, a mirrored local copy of a book is here.   http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html

Attach a Scanner to a File object associated with the file, and read the file, noting the length of each word. Hint: to tell your Scanner to use any non-word characters as a delimiter, you could do something like:

Scanner fin = new Scanner(new File(filename));
fin.useDelimiter("\W+");

(It's not necessary for this assignment, but if you're curious, other possibilities for patterns may be found in thejava.util.regex.Pattern API page.)

Once you've counted the number of words of each size, the program should print a histogram. When I run my program on the Autobiography of Benjamin Franklin, it produces the following histogram:

Do not hard code the name of the file to read. This, instead should be determined at runtime. Either prompt the user for the name of the file (or pass it as a command-line argument). If there is an error opening the file, the program should print an error message and exit. This should be done by handling FileNotFoundException.

We don't know in advance how large the file will be, or the count of words (so, for example, we won't know how long the longest line of '*'s will be), and we don't want the histogram to have lines which wrap. For this reason, the number of words that the '*' represents should be determined only after you've counted all of the words in the file.

Good programming style will play a part in your final grade for the assignment. Please be sure to break up the code into separate functions, and document your code.

what to submit

Send your .java file. It is not necessary to send your practice text files.

Explanation / Answer

main.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class main
{
  
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        try
        {
            translateNumbersInFile("translate.txt");
        }
      
        catch(FileNotFoundException ex)
        {
            System.err.println("File cannot be found");
        }
    }//main
  
  
  
    /**
     * This method is used to take a file and print it into text format.
     *
     * @param filename
     * @throws FileNotFoundException
     */
    public static void translateNumbersInFile(String filename) throws
            FileNotFoundException
    {
       //variables needed for method
       Scanner fin                = new Scanner(new File(filename));
       fin.useDelimiter(" | ");
       NumberTranslator translate = new NumberTranslator();
     
     
       //use while loop to read all numbers
       while(fin.hasNext())
       {
            String s = fin.next();
         
            if (s.length() > 0)
            {
                System.out.println(translate.translate(new Integer(s)));
            }
       }
     
       fin.close();
    }//translateNumbersInFile
}//end

NumberTranslator.java

public class NumberTranslator
{
  
    public String translate(int number)
    {
        //find the length of the number
        Integer numValueAsInteger = number;
        int numberPlace = numValueAsInteger.toString().length();
      
        //declare variable used for holding the number being processed
        int currentValue = 0;
      
        //the holder value for the words describing the int value
        String numberToString = "";
      
        //find value in all places. ex: ones, tens, hundreds.
        for(int i = 0;i< numberPlace;i++)
        {
            if (i > 0)
            {
                numberToString += " ";
            }
            //these hold the two types of numbers handled
          
            currentValue = Integer.parseInt(numValueAsInteger.toString().charAt(i) + "");
          
          
            switch(currentValue)
            {
                case 0:
                    numberToString += "zero";
                    break;
          
                 case 1:
                    numberToString += "one";
                    break;
          
                case 2:
                    numberToString += "two";
                    break;
              
                case 3:
                    numberToString += "three";
                    break;
              
                case 4:
                    numberToString += "four";
                    break;
              
                case 5:
                    numberToString += "five";
                    break;
              
                case 6:
                    numberToString += "six";
                    break;
              
                case 7:
                    numberToString += "seven";
                    break;
              
                case 8:
                    numberToString += "eight";
                    break;
              
                case 9:
                    numberToString += "nine";
                    break;
            }//switch
        }//for
      
      
        return number +": "+ numberToString;
    }
}