私が取り組んでいるプロジェクトで誰かがバグを助けることができるのだろうかと思っていました。私は現在、単語を取り込んで配列に挿入するConcordanceプログラムに取り組んでいます。単語がすでに存在する場合は、単語のカウンターに1が追加されます。最後に、プログラムに単語が何回入力されたかが表示されます。Concordance Javaプログラムが正しい出力を出力しない
第一級が2つの変数文字列とのIntとメソッドのカップルとオブジェクトを作成します。I以下のコードで
は3クラスを持っています。私は問題を抱えているものです
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.
第3クラスは、主なクラスです。複数の単語をハッシュテーブルに挿入して出力をテストしました。ここで
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
出力は次のようになります。
「ワードハローは、テキスト1回
Wordで起動した4回
WordはIDKのテキストで起動しますadcadcがテキストに1回出現する
単語idkがテキストに1回出現する
http://www.tutorialspoint.com/compile_java_online.php?PID=0Bw_CjBb95KQMRkZzalMwb2pLOWMが、私はそれで遊んでてきたこれを見て取るためどうもありがとうございます:
言葉は、私はまた、チュートリアルが私のコードをテストするためにポイントを使用
1回」のテキストで起動しますIDKの私はそれを働かせることはできません。あなたが私のコーディングのスタイルや、私が見なければならないことについてのコメントがあれば、それは大変ありがたいです。