Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

why wont my code work import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class TextStatistics implements TextStatisticsInterface { private File file; private File fileName; private int

why wont my code work import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class TextStatistics implements TextStatisticsInterface { private File file; private File fileName; private int charCount; private int wordCount; private int lineCount; private int letterCount; private int[] lower; private int[] wordLengthCount; private double averageWordLength; private static final String DELIMITERS = "[\\W\\d_]+"; final int NUMCHARS = 26; final int ERROR_CODE = 1; public TextStatistics(File file) {this.file = file; int ERROR_CODE = 1; charCount = 0; wordCount = 0; lineCount = 0; letterCount = 0; wordLengthCount = new int[23]; averageWordLength = 0; int[] upper = new int[NUMCHARS]; int[] lower = new int[NUMCHARS]; try { Scanner fileScan = new Scanner(file); if (file.exists() && file.isFile()) while (fileScan.hasNextLine()) { fileScan.nextLine(); lineCount++; String line = fileScan.nextLine(); Scanner lineScan = new Scanner(line); char current; // the current character being processed int other = 0; // counter for non-alphabetics while (lineScan.hasNextLine()) { for (int ch = 0; ch < line.length(); ch++) { line.toLowerCase(); current = line.toLowerCase().charAt(ch); // if (current >= 'A' && current <= 'Z') { // upper[current - 'A']++; // } else if (current >= 'a' && current <= 'z') { lower[current - 'a']++; } else { other++; } } } for (int letter = 0; letter < upper.length; letter++) { letterCount += lower[letter]; averageWordLength = letterCount / wordCount; } lineScan.useDelimiter(DELIMITERS); while (lineScan.hasNext()) { String words = lineScan.next(); wordCount++; if (words.length() > wordLengthCount.length) { wordLengthCount[words.length()]++; } } while (lineScan.hasNextByte()) { fileScan.nextByte(); charCount++; } } } catch (FileNotFoundException e) { System.out.println("File \"" + this.file + "\" could not be opened."); System.out.println(e.getMessage()); System.exit(ERROR_CODE); } } public File getFileName() { return this.file; } @Override public int getCharCount() { // TODO Auto-generated method stub return charCount; } @Override public int getWordCount() { // TODO Auto-generated method stub return wordCount; } int [] letterCountarray = getLetterCount(); @Override public int getLineCount() { // TODO Auto-generated method stub return lineCount; } @Override public int[] getLetterCount() { // TODO Auto-generated method stub return lower; } @Override public int[] getWordLengthCount() { // TODO Auto-generated method stub return wordLengthCount; } @Override public double getAverageWordLength() { // TODO Auto-generated method stub return averageWordLength; } public String toString() { String output = "stat for " +file ; output += getLineCount() + " lines "; output += getWordCount() + " words "; output += getCharCount() + " characters "; int [] lots = getLetterCount(); output += "a = " + lots[0] + " " + lots[12] + " "; output += "b = " + lots[1] + " " + lots[13] + " "; output += "c = " + lots[2] + " " + lots[14] + " "; output += "d = " + lots[3] + " " + lots[15] + " "; output += "e = " + lots[4] + " " + lots[16] + " "; output += "f = " + lots[5] + " " + lots[17] + " "; output += "g = " + lots[6] + " " + lots[18] + " "; output += "h = " + lots[7] + " " + lots[19] + " "; output += "j = " + lots[8] + " " + lots[20] + " "; output += "k = " + lots[9] + " " + lots[21] + " "; output += "l = " + lots[10] + " " + lots[22] + " "; output += "m = " + lots[11] + " " + lots[23] + " "; output += "a = " + lots[12] + " " + lots[24] + " "; output += String.format("%s=%s", " +words.length +wordLengthCount")+" "; output += " "; output += "average word length "+ getAverageWordLength(); return output; } } import java.io.File; public class ProcessText { public static void main(String[] args) { if (args.length == 0) { System.out.println("Usage: Java ProcessText file1 [file2...]"); System.exit(0); } for(int i=0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Donald A. Carpenter Fred R. McFadden

1st Edition

8178088045, 978-8178088044

More Books

Students also viewed these Databases questions