Question
I need Java help. I am getting the following errors Project3.java:65: error: ';' expected for(int x = 0); x expected for(int x = 0); x
I need Java help. I am getting the following errors
Project3.java:65: error: ';' expected for(int x = 0); x expected for(int x = 0); x
I know semicolons are not supposed to be there. The help is appreciated
My output should be this from a dictionary file with over 1000 words.
My instructions:
You will load the dictionary into the array and resize the array (by doubling its current .length) as needed anytime it gets full. As each word is read in you will also update an array of frequency counters that record how many words of length 1 or length etc were in the file. When your program is finished this array will contain the following: at index [1], the number words of length 1. At index [2] then number of words of length 2, and thus at histogram[n] will be stored the number of words of length n. Since there will be no words of length zero, there will be a zero in histogram[0]. The [0] slot is thus ignored/wasted but that is a small price to pay to avoid off by one dissonance between word length and index.
The histogram is just a plain array of int that is initialized to length zero. Recall that Java allows us to define a new array with a length of zero. It happens every time you run a Java program with no command line arguments. Doing so initializes the args array to have length zero. Every time you read a word from the dictionary you cannot just execute an increment statement such as
++histogram[word.length()];
- This is the correct statement to execute, but you must first make sure the histogram is long enough to have a cell at that index. If your current word has length of 7, you must first make sure that your histogram array has a .length of at least 8 (not 7) because the [7] cell of the array is actually the eighth cell.
Whenever you encounter a word whose length is >= the length of your frequency counter array, upsize your counter array to be just big enough (length+1) for that particular length value (length+1). At the end of the program it is possible that you could have gaps in your histogram if (depending on the input file) there were no words of length 7 or 13 or 26 and so on. In this case there would still be a zero at index 7 or 13 or 26.
Here is my code
import java.io.*; import java.util.*;
public class Project3 { static final int INITIAL_CAPACITY = 10; public static void main (String[] args) throws Exception { // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length java Project2 "); // i.e. C:\> java Project2 dictionary.txt System.exit(0); } int[] histogram = new int[0]; // histogram[i] == # of words of length n
/* array of String to store the words from the dictionary. We use BufferedReader (not Scanner). With each word read in, examine it's length and update word length frequency histogram accordingly. */
String[] wordList = new String[INITIAL_CAPACITY]; int wordCount = 0; BufferedReader infile = new BufferedReader( new FileReader(args[0]) ); while ( infile.ready() ) { String word = infile.readLine(); // # # # # # DO NOT WRITE/MODIFY ANYTHING ABOVE THIS LINE # # # # #
// test to see if list is full. If needed do an up size (just like Lab#3) if(wordCount == wordList.length) { wordList= upSizeArr(wordList); }// now you may safely append word onto list and incr count word = wordList[wordCount]; wordCount++; // look at the word length and see if the histogram length is AT LEAST // word length + 1. If not, you must upsize histogram to be EXACTLY word length + 1 // now you can increment the counter in the histogram for this word's length if(word.length() >= histogram.length) { histogram = upSizeHisto(histogram, word.length()); histogram[word.length()]++; } // # # # # # DO NOT WRITE/MODIFY ANYTHING BELOW THIS LINE # # # # # } //END WHILE INFILE READY infile.close();
wordList = trimArr( wordList, wordCount ); System.out.println( "After final trim: wordList length: " + wordList.length + " wordCount: " + wordCount );
// PRINT WORD LENGTH FREQ HISTOGRAM for ( int i = 0; i
} // END main
// YOU MUST CORRECTLY COPY THE STRING REFS FROM THE OLD ARR TO THE NEW ARR static String[] upSizeArr( String[] fullArr ) { int newInt = (fullArr.length)*2; String[] arrString = new String[newInt]; for(int x = 0); x
// YOU MUST CORRECTLY COPY THE COUNTS FROM OLD HISTO TO NEW HISTO static int[] upSizeHisto( int[] oldArr, int newLength ) { int arrInt[] = new int[newLength]; for(int x = 0; x After final trim: wordList words of length 0 0 words of length 1 0 words of length 2 96 words of length 3 972 words of length 4 3903 words of length 5 8636 words of length 6 15232 words of length 723109 words of length 8 28419 words of length 9 24873 words of length 10 20302 words of length 11 15504 words of length 12 11358 words of length 13 7827 words of length 14 5127 words of length 15 3192 words of length 16 1943 words of length 17 1127 words of length 18 594 words of length 19 329 words of length 20 160 words of length 21 62 words of length 22 30 words of length 23 13 words of length 24 words of length 25 2 words of length 260 words of length 27 2 words of length 28 1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started