【发布时间】:2015-02-26 03:01:39
【问题描述】:
我已经解决了编译问题,但我的升迁直方图方法遇到了问题。似乎我的方法的“复制”部分由于某种原因不起作用。有任何想法吗?我会在代码中标记出来。
这是我编写的全部代码。
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
我可以使用的最后一点帮助是代码中的最后一个方法。由于错误,我尚未对其进行测试,但如果有任何问题,我可以使用帮助。
谢谢大家。
【问题讨论】:
-
我认为您没有为此使用任何 IDE。 :)
-
是的,使用 Eclipse。但它没有发现任何错误。
标签: java methods compiler-errors histogram