我想知道是否有人可以帮助解决我正在进行的项目中遇到的错误。我目前正在开发一个Concordance程序,它接受单词并将它们插入到数组中。如果该单词已经存在,则将该单词的计数器加1。最后,它将显示一个单词输入程序的次数。
在下面的代码中,我有3个类:
第一类创建一个具有2个变量的对象,一个String,一个Int和几个方法。
public class Concordance
{
private String word;//Stores a word
private int counter = 0;//Stores how many times a word comes up.
/**
* Constructor:
*
* Gives a default value to word if nothing is put in.
*/
public Concordance()
{
word = "";
}
/**
* Constructor
*
* Takes users input and sets it equal to word.
* Adds 1 to counter
*
* @param String input
*/
public Concordance( String input )
{
word = input;
counter = 1;
}
/**
* Adds a 1 to a word's counter
*/
public void addCounter()
{
this.counter++;
}
/**
* Returns the string of the object.
*
* @return word
*/
public String getString()
{
return word;
}
/**
* Returns the number of times the word was put in.
*
* @return counter
*/
public int getCount(){
return counter;
}
}
我遇到问题的第二个类,在第一个类中创建了一个对象数组,并且一些方法将检查该单词是否已经存在于数组中。如果是,它将向该单词的计数器加1,否则它将该单词添加到数组的末尾。 insertWord()方法给了我一个奇怪的输出。 for循环内部的条件语句似乎没有正常工作。当我插入多个单词时,它只会多次识别第一个单词,但之后的每个单词只会在数组的末尾添加,只需添加计数器。
此类还有一个打印数组和构造函数的方法。
public HashTable()
{
Concordance temp = new Concordance();
for(int i = 0; i < 255; i++ )
{
arrayWords[i] = temp;
}
arrayCounter = 0;
}
/**
* insertWord()
*
* Checks if the word is already in the array.
* If it is in the Array it will add 1 to the counter,
* if not it will add it to the end of the array.
*
* @param String word
*/
public void insertWord( String word )
{
for( int i = 0; i < arrayWords.length; i++ )
{
System.out.println(i);
/*Checks if the word is already in the array.
*If it is,it will add 1 to the counter of the word
* or else it will add the word to the end of the array.
*/
if( word.toLowerCase() == arrayWords[i].getString() )
{
System.out.println("Adding to Counter to word " + word );
arrayWords[i].addCounter();//Adds 1 to the counter of the word.
break;
}
else
{
System.out.println("Adding " + word +" to Array" );
Concordance temp = new Concordance(word);
//Adds the word at the end of the array
arrayWords[arrayCounter] = temp;
arrayCounter++;//Updates the counter array.
break;
}//End of Conditional statement.
}//End of for loop
}//End of insertWord method
/**
* printWords() method
*
* It prints every Concordance object with their string and counter.
*/
public void printWords()
{
System.out.print("\nprinting words\n");
for( int i = 0; i < arrayCounter; i++ )
{
System.out.println( "The Word " + arrayWords[i].getString() + " comes up in the text " + arrayWords[i].getCount() + " times" );
}
}
}//End of Hash Table Class.
第三类只是我测试输出的多个单词到Hash Table的主要类。
public class Main {
public static void main( String [] arg )
{
//Instantiates a Concordance object.
Concordance c = new Concordance( "hello" );
//Prints the string of the object.
System.out.print(c.getString());
//Prints the counter of the object.
System.out.println(c.getCount());
//Instantiates a hash table
HashTable ht = new HashTable();
//Inserts different values to the Hash Table
ht.insertWord("hello");
ht.insertWord("hello");
ht.insertWord("there");
ht.insertWord("how");
ht.insertWord("hello");
ht.insertWord("are");
ht.insertWord("you");
ht.insertWord("there");
ht.printWords();
}//end of static void
以下是输出结果:
“Word hello 4次出现在文本中
Word idk在文本中出现了1次
Word adcadc在文本中出现了1次
Word idk在文本中出现了1次
Word idk在文本中出现了1次“
我无法让它发挥作用。
答案 0 :(得分:0)
虽然您可以使用java.util.HashMap
完成此操作,但您可以使用自己的课程,但如果您希望使用自己的课程,那么以下几点可能对您有所帮助。
您已经创建了一个类HashMap<String,Integer> wordCountMap
,但您没有使用HashTable
,因此您应该更改类名或修改逻辑以使用散列。但是现在让我们继续使用相同的类名并尝试纠正现有的逻辑。
在类hashing technique
的构造函数中,您正在创建一个HashTable
的对象,并为其分配每个单元格。修改如下。
Concordance
现在让我们尝试修复插入单词的逻辑。
public HashTable()
{
// create an Object of array of type Concordance.
//Let each cell reference to null.
arrayWords = new Concordance[255];
arrayCounter = 0;
}
现在继续使用private int arrayCounter = 0;
public void insertWord(String word) {
if(word == null){
return;
}
boolean isWordPresent = false;
// SCAN the whole array and see if the word is present in it else after
// the loop ends then add new word
// Also you just need to check till 'arrayCounter' as it will always be equal to number of elements in the array
for (int i = 0; i < arrayCounter; i++) {
System.out.println(i);
// equalsIgnoreCase in class java.lang.String returns null if
// argument passed is Null, so extra check for arrayWords[i] ! =
// null is not required
if (word.equalsIgnoreCase(arrayWords[i])) {
arrayWords[i].addCounter();
isWordPresent = true;
break;
}
}
// in case word is not found above then add it as a new word
if (!isWordPresent) {
System.out.println("Adding " + word + " to Array");
arrayWords[arrayCounter++] = new Concordance(word);
}
}
方法,HashTable类创建类Concordance的必要对象。您在main方法中创建的类Concordance的对象将不会用于逻辑。最好删除以下代码。
main
其他评论可能对您将来有所帮助。