Ok, so, I'm a relatively new programmer and I'm having major difficulty with this assignment. The assignment is to create a 2 class java code that will read in a file, a book specifically, and will analyze that to count average sentence length and average word length. The correct output should look something like this:
> java WordMapDriver
enter name of a file
Analyzed text: /Users/moll/CS121/AliceInWonderland.txt
words of length 1: 7.257%
words of length 2: 14.921%
words of length 3: 24.073%
words of length 4: 20.847%
words of length 5: 12.769%
words of length 6: 7.374%
words of length 7: 6.082%
words of length 8: 3.012%
words of length 9: 1.812%
words of length 10: 0.820%
words of length 11: 0.501%
words of length 12: 0.236%
words of length 13: 0.134%
words of length 14: 0.083%
words of length 15 or larger: 0.001%
average sentence length: 16.917
I have been working on this code for a while but when I run it, I just get a lot of angry red error messages. Here is the long list of complaints:
java.lang.StringIndexOutOfBoundsException: String index out of range: 239
at java.lang.String.charAt(Unknown Source)
at WordMap.processLine(WordMap.java:26)
at Echo.readLines(Echo.java:16)
at WordMapDriver.main(WordMapDriver.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.dynamicjava.symbol.JavaClass$JavaMethod.evaluate(JavaClass.java:362)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.handleMethodCall(ExpressionEvaluator.java:92)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.visit(ExpressionEvaluator.java:84)
at koala.dynamicjava.tree.StaticMethodCall.acceptVisitor(StaticMethodCall.java:121)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:38)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:37)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:106)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:29)
at koala.dynamicjava.tree.ExpressionStatement.acceptVisitor(ExpressionStatement.java:101)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.evaluateSequence(StatementEvaluator.java:66)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.evaluate(Interpreter.java:77)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.interpret(Interpreter.java:47)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:246)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:220)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
And here is my code. Any help I can get is greatly appreciated:
1 import java.util.Scanner;
2 import java.io.*;
3
4 public class Echo{
5 String fileName; // external file name
6 Scanner scan; // Scanner object for reading from external file
7
8 public Echo(String f) throws IOException
9 {
10 fileName = f;
11 scan = new Scanner(new FileReader(fileName));
12}
13
14 public void readLines(){ // reads lines, hands each to processLine
15 while(scan.hasNext()){
16 processLine(scan.nextLine());
17 }
18 scan.close();
19 }
20
21 public void processLine(String line){ // does the real processing work
22 System.out.println(line);
23}
24}
___________________
1 import java.util.*;
2 import java.io.*;
3 public class WordMap extends Echo {
4 String fileName; //external text file name
5 Scanner scan; //scanner object to read external text
6 int sentencecount = 0;
7 int wordcount = 0;
8 int charcount = 0;
9 //creates an array that will be used to count average word length
10 double[] wlength = new double[15];
11 int fifteenless = 0;
12 int fifteenplus = 0;
13 public WordMap(String f) throws IOException
14 {
15 super(f);}
16
17 public void processLine(String line)
18 //creates a string array of every word
19 {String [] words = line.split("\\s+");
20 for(int a = 0; a < words.length; a++){
21 words[a] = words[a].replaceAll("[\\w]","");}
22
23 //counts words
24 wordcount = words.length;
25
26 //counts sentences by searching for sentence enders
27 for(int c = 0; c < line.length(); c++){
28 char ca = line.charAt(c);
29 if((line.charAt(ca) == '.')|| (line.charAt(ca) =='!') || (line.charAt(ca) =='?'));
30 sentencecount++ ;}
31
32 //if wordlength matches, adds one to wlength at d and to words less than 15 chars
33 for(int d = 1; d < 15; d++){
34 for(int g = 0; g < words.length; g++){
35 String temp = words[g];
36 if(d == temp.length()){
37 wlength[d]++;
38 fifteenless++;}}}}
39
40 public void calculatePercentages(){
41 {//calculate percentages with words of 15 or more characters
42 fifteenplus = wordcount - fifteenless;
43 fifteenplus = fifteenplus / wordcount;
44 fifteenplus = fifteenplus*100;}
45
46 //calculate percentages for words with 1-14 characters
47 for(int h = 1; h<15; h++){
48 wlength[h] /= wordcount;
49 wlength[h]*=100;}
50 }
51 public void reportLength(){
52 //prints out average sentence length
53 double length = wordcount / sentencecount;
54 System.out.printf("average sentence length: "+ "%5.3f",length);}
55
56 public void getWordLength(){
57 //prints out word of length percentages with a loop for 1-14 and a final one for 15+
58 for(int i = 1; i <15; i++){
59 System.out.printf("words of length " + i + ": %5.3f%%\n",wlength[i]);}
60 System.out.printf("words of 15 or more characters length: " + "%5.3f%%\n",fifteenplus);
61 }}
_______
1 import java.util.*;
2 public class WordMapDriver {
3 public static void main(String[] args) {
4 try{
5 String fileName;
6 Scanner textfile = new Scanner(System.in);
7 System.out.println("enter name of file");
8 fileName = textfile.nextLine(); //scans in file
9 WordMap k = new WordMap(fileName);//creates wordmap object
10 k.readLines(); //processes
11 k.calculatePercentages();
12 k.getWordLength(); //reports all wordlength percentages
13 k.reportLength(); //prints out average sentence length
14 }
15 catch(Exception e) //catches the ioexception
16 {e.printStackTrace();
17 }}}
*EDIT: I added line numbers. Also, this may be dumb, but I don't know what SSCCE **EDIT2: Ok, so I followed advice and now it prints this followed by more angry red messages:
words of length 1: 50620.000%
words of length 2: 15690.000%
words of length 3: 2020.000%
words of length 4: 300.000%
words of length 5: 620.000%
words of length 6: 70.000%
words of length 7: 20.000%
words of length 8: 10.000%
words of length 9: 0.000%
words of length 10: 10.000%
words of length 11: 10.000%
words of length 12: 10.000%
words of length 13: 10.000%
words of length 14: 0.000%
words of 15 or more characters length: java.util.IllegalFormatConversionException: f != java.lang.Integer
Look at this section:
for(int c = 0; c < line.length(); c++){
char ca = line.charAt(c);
if((line.charAt(ca) == '.')|| (line.charAt(ca) =='!') || (line.charAt(ca) =='?'));
sentencecount++ ;}
There are two problems here:
The variable "ca" is the character at index c. But you're passing "ca" to line.charAt... Java is "helpfully" converting the char to an int, and trying to return the character at that index. That's not what you want, and it's why you're getting the StringIndexOutOfBoundsException.
You probably want to increment sentenceCount only if the expression is true, right? That's not what the code says. This "if" statement has no body (because of the semicolon on the same line as the "if" statement). A good practice, especially for beginners, is to always use curly braces to enclose the body of if blocks, even if they are only one line.
As for the IllegalFormatConversionException, it's telling you that %5.3f is not a valid conversion pattern for an integer ("f" is for floats and doubles). fifteenplus is declared as an int; is that the correct type? If so, you can just print it like this:
System.out.println("words of 15 or more characters length: " + fifteenplus);
If you mean for it to be a percentage like all the other lines, then you should declare it as a float or double (generally you should prefer doubles over floats because doubles offer more precision and the performance difference is usually insignificant).
One final point: Isn't it strange that all of your percentages end in .000%? In Java, dividing an int by an int always gives you an int. If you want a floating point result, you must cast one of the arguments to a float or double, like this:
int a = 1;
int b = 2;
double result = (double) a / b;
Try that with and without the cast to double to see the difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With