2016-09-10 13 views
-1

私が取り組んでいるプロジェクトで誰かがバグを助けることができるのだろうかと思っていました。私は現在、単語を取り込んで配列に挿入する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の私はそれを働かせることはできません。あなたが私のコーディングのスタイルや、私が見なければならないことについてのコメントがあれば、それは大変ありがたいです。

答えて

0

HashMap<String,Integer> wordCountMapを受け取った可能性があるjava.util.HashMapを使用しても問題はありませんが、自分のクラスを使用したい場合は、以下の点が参考になります。

クラス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); 
    } 
} 

今すぐあなたのmain方法に移り、ハッシュテーブルクラスは、クラスのコンコーダンスの必要なオブジェクトを作成します。メインメソッドのConcordanceクラスの作成したオブジェクトは、ロジックには使用されません。以下のコードを削除してください。

 //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()); 

その他のレビュー今後お役に立つかもしれないポイント。

  1. [あなたはハッシュテーブルを使用している]のJavaによって提供されるものと同じ、あなたのクラスに名前を付けないでください、クラス、メソッド、フィールドまたはローカル変数の名前を正当化する必要があります。 HashTableという名前を使用していますが、実際にはHashingを使用していないので、名前が誤解を招くことがあります。
  2. 必要なコメントを入力し、余分を置かないでください。あなたのコードエンティティに適切な文脈名を使用して、名前自体が説明のために十分であるようにしてください。
  3. ロジックで使用しない不要なオブジェクトを作成しないでください。他の人があなたのコードを見ている間、それらは必要ではなく、混乱を招きます。
  4. ロジックを単純なままにし、可能であれば小さくするようにしてください。コードを読みやすく、保守しやすくしてください。
関連する問題