【发布时间】:2018-09-19 20:36:18
【问题描述】:
我在尝试将一个单词添加回单词列表时遇到了一些麻烦。
程序会计算单词的长度,然后将其存储起来,以便输出显示如下内容:
单词长度 7 56
我知道了,所以它可以正确计算字数,但输出没有将正确的字数和正确的字数放在一起。
应该是这样的 长度为 1 0 的单词
但我的节目 字长 1 97
(这是长度为 2 的单词的正确计数)。
我不确定如何解决这个问题。
我觉得应该是这样的:
wordList[wordCount-1] = word;
(-1 是这样我不会得到 Array out of bounds 错误)。
import java.io.*;
import java.util.*;
public class Project2
{
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 < 1 )
{
System.out.println("\nusage: C:\\> java Project2 <input filename>\n\n"); // 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 # # # # #
if (wordCount == wordList.length)
wordList = upSizeArr(wordList);
// test to see if list is full. If needed do an up size (just like Lab#3)
wordList[wordCount++] = word;
// now you may safely append word onto list and incr count
int wordLength = word.length();
if (word.length () > histogram.length)
histogram = upSizeHisto(histogram, wordLength);
// 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
histogram[word.length()-1]++;
// now you can increment the counter in the histogram for this word's 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 < histogram.length ; i++ )
System.out.format("words of length %2d %d\n", i,histogram[i] );
} // END main
// YOU MUST CORRECTLY COPY THE STRING REFS FROM THE OLD ARR TO THE NEW ARR
static String[] upSizeArr( String[] fullArr )
{
String [] newArr = new String [fullArr.length*2];
for (int count = 0; count < fullArr.length ; count++)
{
newArr[count] = fullArr[count];
}
return newArr; // just to make it complie you change as needed
}
static String[] trimArr( String[] oldArr, int count )
{
String[] newArr = new String[count];
for ( count = 0; count < newArr.length ; count++)
{
newArr[count] = oldArr[count];
}
return newArr; //return null; // just to make it complie you change as needed
}
// YOU MUST CORRECTLY COPY THE COUNTS FROM OLD HISTO TO NEW HISTO
static int[] upSizeHisto( int[] oldArr, int newLength )
{
int [] newHisto= new int[newLength];
if (oldArr.length > 1)
{
for (int count = 0; count < oldArr.length ; count++)
{
newHisto[count] = oldArr[count];
}
}
return newHisto; // just to make it complie you change as needed
}
} // END CLASS PROJECT#2
问题:如何将单词附加到单词列表数组中(单词列表来自文本文件)。不使用数组或哈希。
【问题讨论】:
-
请展示更多您的代码。
-
发布您的代码。
-
已更新。请,完整的代码运行良好。这只是我无法弄清楚的很小的一部分。