我已经解决了编译问题并且遇到了我的upsize直方图方法的问题。似乎我的副本'由于某种原因,该方法的某些部分不起作用。有任何想法吗?我会在代码中标记它。
以下是我编写的全部代码。
import java.io.*;
import java.util.*;
public class Project5
{
static final int INITIAL_CAPACITY = 10;
public static void main (String[] args) throws Exception
{
if (args.length < 1) die("You must type the dictionary filename on cmd line.\n");
// Here we have declared an int array, called 'histogram' with initial capacity of 0
// it is a freq counter to word lengths in the file
int[] histogram = new int[0];
// Here we have declared an array of String to read the dictionary file into. 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() ) // i.e. while there are more lines of text in the file
{
String word = infile.readLine();
// YOUR CODE HERE TO CHECK TO SEE IF WORDLIST IS FULL
// IF SO YOU MUST DO AN UPSIZE JUST LIKE LAB#6
if ( wordCount == wordList.length )
wordList = upSizeArr( wordList );
// YOUR CODE HERE to add this word to your list
String newWord = infile.next();
wordList[wordCount++] = newWord;
// YOUR CODE HERE TO LOOK AT THE LENGTH OF THE WORD AND UPDATE HISTOGRAM
int wordLength = word.length;
if (word.length() > histogram.length)
histogram = upSizeHisto( histogram, wordLength );
histogram[word.length()]++;
// example if word.length() is 5 then histogram[5] gets increment
// BUT IF WORD LENGTH IS >= HISTORGRAM LENGTH
// THEN YOU NEED TO FIRST CALL upSizeHisto TO UPSIZE THE HISTOGRAM TO BE OF EXACTLY LENGTH word.length()+1
// SIMILAR TO HOW YOU HAD TO UPSIZE WORDLIST
} //END WHILE INFILE READY
infile.close();
wordList = trimArr( wordList, wordCount );
System.out.println( "After trim, wordList length: " + wordList.length );
// PRINT WORD LENGTH FREQ HISTOGRAM
for ( int i = 0; i < histogram.length ; i++ )
System.out.println("words of length " + i + ": " + histogram[i]);
} // END main
private static void die( String msg )
{
System.out.println( msg );
System.exit(0);
}
private static String[] upSizeArr( String[] oldArr )
{
String[] newArr = new String[oldArr.length * 2];
for (int cnt = 0; cnt < oldArr.length; cnt++)
{
newArr[cnt] = oldArr[cnt];
}
return newArr;
}
private static String[] trimArr( String[] oldArr, int count )
{
String[] newArr = new String[count];
for (int cnt = 0; cnt < newArr.length; cnt++)
{
newArr[cnt] = oldArr[cnt];
}
return newArr;
}
private static int[] upSizeHisto( int[] oldArr, int newLength )
{
int[] newHisto = new int[newLength];
for (int cnt = 0; cnt < newHisto.length; cnt++)
{
newHisto[cnt] = oldArr[cnt]; // ERROR HERE, Any help guys?
}
return newHisto;
}
} // END CLASS PROJECT#5
我可以使用的最后一点帮助是代码中的最后一个方法。由于错误,我还没有对它进行测试,但如果有任何问题,我可以使用帮助。
谢谢你们。
答案 0 :(得分:0)
几个错误。
1)
int wordLength = word.length;
到
int wordLength = word.length();
2)。
String newWord = infile.next();
到
String newWord = infile.readLine();
3.)添加Try Catch
代码如下public class Project5 {
static final int INITIAL_CAPACITY = 10;
public static void main(String[] args) {
if (args.length < 1)
die("You must type the dictionary filename on cmd line.\n");
// Here we have declared an int array, called 'histogram' with initial capacity of 0
// it is a freq counter to word lengths in the file
int[] histogram = new int[0];
// Here we have declared an array of String to read the dictionary file into. 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;
try {
BufferedReader infile = new BufferedReader(new FileReader(args[0]));
while (infile.ready()) // i.e. while there are more lines of text in the file
{
String word = infile.readLine();
// YOUR CODE HERE TO CHECK TO SEE IF WORDLIST IS FULL
// IF SO YOU MUST DO AN UPSIZE JUST LIKE LAB#6
if (wordCount == wordList.length)
wordList = upSizeArr(wordList);
// YOUR CODE HERE to add this word to your list
String newWord = infile.readLine();
wordList[wordCount++] = newWord;
// YOUR CODE HERE TO LOOK AT THE LENGTH OF THE WORD AND UPDATE HISTOGRAM
int wordLength = word.length();
if (word.length() > histogram.length)
histogram = upSizeHisto(histogram, wordLength);
histogram[word.length()]++;
// example if word.length() is 5 then histogram[5] gets increment
// BUT IF WORD LENGTH IS >= HISTORGRAM LENGTH
// THEN YOU NEED TO FIRST CALL upSizeHisto TO UPSIZE THE HISTOGRAM TO BE OF EXACTLY LENGTH word.length()+1
// SIMILAR TO HOW YOU HAD TO UPSIZE WORDLIST
infile.close();
} // END WHILE INFILE READY
} catch (Exception e) {
}
wordList = trimArr(wordList, wordCount);
System.out.println("After trim, wordList length: " + wordList.length);
// PRINT WORD LENGTH FREQ HISTOGRAM
for (int i = 0 ; i < histogram.length ; i++)
System.out.println("words of length " + i + ": " + histogram[i]);
} // END main
private static void die(String msg) {
System.out.println(msg);
System.exit(0);
}
private static String[] upSizeArr(String[] oldArr) {
String[] newArr = new String[oldArr.length * 2];
for (int cnt = 0 ; cnt < oldArr.length ; cnt++) {
newArr[cnt] = oldArr[cnt];
}
return newArr;
}
private static String[] trimArr(String[] oldArr, int count) {
String[] newArr = new String[count];
for (int cnt = 0 ; cnt < newArr.length ; cnt++) {
newArr[cnt] = oldArr[cnt];
}
return newArr;
}
private static int[] upSizeHisto(int[] oldArr, int newLength) {
int[] newHisto = new int[newLength];
for (int cnt = 0 ; cnt < newHisto.length ; cnt++) {
newHisto[cnt] = oldArr[cnt];
}
return newHisto;
}
}