So, my assignment is to write a program that takes a file as an input and give some statistics on the file. Right now, I'm just trying to find the average number of words per sentence.
I extended the Echo class,
import java.util.Scanner; import java.io.*; public class Echo{ String fileName; // external file name Scanner scan; // Scanner object for reading from external file public Echo(String f) throws IOException { fileName = f; scan = new Scanner(new FileReader(fileName)); } public void readLines(){ // reads lines, hands each to processLine while(scan.hasNext()){ processLine(scan.nextLine()); } scan.close(); } public void processLine(String line){ // does the real processing work System.out.println(line); } }
With my class WorldMap,
import java.io.*; import java.util.*; public class WorldMap extends Echo{ int numWords = 0; int numSentence = 0; public WorldMap(String f) throws IOException{ super(f); } public double getAvg(){ return ((double)numWords/numSentence); } public void processLine(String line){ try{ StringTokenizer sentence = new StringTokenizer(line, ".?,"); // Breaks line by punctuation, into sentences StringTokenizer words = new StringTokenizer(line, " "); // Breaks line by spaces, into words while (words.hasMoreTokens()){ numWords++; } while (sentence.hasMoreTokens()){ numSentence++; } } catch (Exception e){ System.out.println("Invaid line: Line " + line); } } }
& Here is my main, driver class
import java.util.*; import java.io.*; public class WorldMapDriver{ public static void main(String[] args) throws IOException{ try{ Scanner scan = new Scanner(System.in); System.out.println("Enter the name of a text file"); String file = scan.next(); WorldMap world = new WorldMap(file); world.readLines(); world.getAvg(); } catch (FileNotFoundException e){ System.out.println("File not found"); } } }
I don't know much about reading input files, but I can't see why this program won't work. When I enter the location of a file, nothing happens... the client just sort of freezes like its still thinking.
The code compiles fine, with no errors or warnings.
Any help is greatly appreciated!