0

I've been working on an assignment for quite sometime now. The program compiles fine, but when ran, the driver class does not produce any results. The program I'm writing extends another class and is used to find the average word length of a text file as well as how often words with one letter, two letters, three letters, etc appear (any word that is 15 or greater letters is grouped).

Here is the class of which mine extends:

public abstract class FileAccessor{
  String fileName; 
  Scanner scan;

  public FileAccessor(String f) throws IOException{
    fileName = f;
    scan = new Scanner(new FileReader(fileName));
  }

  public void processFile() { 
    while(scan.hasNext()){
      processLine(scan.nextLine());
    }
    scan.close();
  }

  protected abstract void processLine(String line);

  public void writeToFile(String data, String fileName) throws IOException{
        PrintWriter pw = new PrintWriter(fileName);
      pw.print(data);
      pw.close();
   }
}

Here is my work:

import java.util.Scanner;
import java.io.*;

public class WordPercentages extends FileAccessor{
   int[] length1 = new int[15];
   double[] percentages = new double[15];
   int totalWords = 0;
   double average = 0.0;


   public WordPercentages(String s)throws IOException{
      super(s);
      }
   public void processLine(String file){
      super.fileName=file;
      while(super.scan.hasNext()){
         totalWords+=1;
         String s = super.scan.next();
         if (s.length() < 15){
            length1[s.length()]+=1;
            }
         else if(s.length() >= 15){
            length1[15]+=1;
            }
      }
   }

   public double[] getWordPercentages(){
      for(int j = 1; j < percentages.length; j++){
         percentages[j] += length1[j];
         percentages[j]=(percentages[j]/totalWords)*100;
         }
      return percentages; 
      }
   public double getAvgWordLength(){
      for(int j = 1; j<(percentages.length); j++){
         average+=((j*(percentages[j])/totalWords));
         }
      return average;
      }
}

And here is the driver class:

import java.util.Scanner;
import java.io.IOException;

public class WordPercentagesDriver{
  public static void main(String[] args) throws IOException{
      try{
          String fileName;
          Scanner scan = new Scanner(System.in);
          System.out.println("Enter a text file name to analyze:");
          fileName = scan.nextLine();
          System.out.println("Analyzed text: " + fileName);
          WordPercentages wp = new WordPercentages(fileName);
          wp.processFile();
          double [] results = wp.getWordPercentages();
          printWordSizePercentages(results);
          System.out.printf("average word length: %4.2f",wp.getAvgWordLength());
      }
      catch(Exception e)
      {
          System.out.println(e);
      }
  }

  public static void printWordSizePercentages(double[] data){
      for(int i = 1; i < data.length; i++)
        if (i==data.length-1)
           System.out.printf("words of length " + (i) + " or greater: %4.2f%%\n",data[i]);
        else
            System.out.printf("words of length " + (i) + ": %4.2f%%\n",data[i]);
  }
}

I've tried placing a text file with known results in the same folder, everything complies, I then type in the name of the text file (including the .txt) and unfortunately nothing happens. Any help would be greatly appreciated.

NOTE: The FileAccessor Class and the Driver class were both provided by my instructor, so any source of error would come from the WordPercentage class.

3
  • 2
    Two things. 1) Don't use stack snippets for Java. Java is not Javascript. 2) No-one is going to read all this mass of code - this is a classic "why isn't this code working" question. Post an SSCCE. Apr 15, 2015 at 21:48
  • 2
    P.S. to isolate your problem, learn to use a debugger. Apr 15, 2015 at 21:51
  • 1
    What do you mean by 'nothing happens'? Do you even see the line "Analyzed text"? Or are you expecting and not seeing the 'words of length' lines?
    – Aurora
    Apr 15, 2015 at 22:36

1 Answer 1

0

Just ran the code, It looks like your code is throwing a "FileNotFoundExeption". What you have to do to fix this is place the file that you are looking for into the workspace you are working in. You can either save the txt file into your workspace or specify where the txt file is located. Example: C:Programs/documents/Alice... good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.