2017-12-08 13 views
0

私はこのアルゴリズムを完了しようとしています。このアルゴリズムは、txtファイルのスペルミスのある単語を置換する単語に置き換えることになっています。ご覧のとおり、アルゴリズムは上記のコメントで説明されており、最初の2つの部分が完了しましたが、for each line number where it appears:部分に固執しています。私は、ループを設定する方法を理解しようとする際に問題を抱えています。誰かがこの方法を終了する方向に私を助けることができたら、私はそれを非常に感謝します!あなたが役に立つかもしれません辞書のスペルミスの単語を置き換えてください

private void replace(String misspelled, String replacement){ 
    //TODO: Algorithm: 
    //If wrongWords contains the misspelled word: 
    // get ALL the lineNumbers on where the misspelled word appears 
    // for each line number where it appears: 
    //  in fileLines[line] replace misspelled with replacement 
    //  (Hint: use one of the available methods in the String class to do the replacement) 
    if(wrongWords.containsKey(misspelled)) 
     wrongWords.get(misspelled); 

} 

その他の注目すべきコードが含まれています:ユーザーならば

private void correctionMode(){ 
    for(String line: fileLine) 
    for(String w: line.split("//s")) 
     if(wrongWords.containsKey(w)){ 
      System.out.println(wrongWords.get(w)); 
      System.out.println("replace all? (y or n): "); 
      String r = scan.nextLine(); 
      if(r.equals("y")){ 
       System.out.println("Enter replacement: "); 
       String r2 = scan.nextLine(); 
       replace(w, r2); 
      } 
      wrongWords.remove(w); 
     }       
} 

この方法は、修正のためにユーザーに確認してから交換方法、私は問題を抱えています1に行きます変更を加えたい

private Scanner scan; // a Scanner to read user's input 
private HashSet<String> dictionary; 
private HashMap<String, ArrayList<Integer>> wrongWords; 
private ArrayList<String> fileLine; 

\\constructor 
public SpellChecker(){ 
    scan = new Scanner(System.in); 
    dictionary = new HashSet<String>(); 
    wrongWords = new HashMap<String, ArrayList<Integer>>(); //array is line numbers where misspelled word appears 
    fileLine = new ArrayList<String>(); //each line is as separate element in the arraylist 
} 

クラス変数とコンストラクタも役立ちます。

+0

は、あなたが単語が誤って綴られていることを与えられているか、私たちにサンプルを示してもらえますか? 2-3行のように。 – vikiiii

+0

@vikiiii twinkle twinkle lottle starどのように私は 蓮の羊の笑いの世界a。 涙の世界。例えば ​​"lottle"は辞書にないので、間違って入力されていると考えられます。 –

+0

'line.split(" // s ")'はあなたが望むことをしません。スラッシュとバックスラッシュは同じではありません。おそらく '' \\ s "'を意味します。 – VGR

答えて

0

各行の両方のメソッドにforループを入れる必要はありません。

// for each line number where it appears: 

for(String line: fileLine) 

はそれのいずれかにそれを置く:

private void replace(String misspelled, String replacement){ 
     int i = 0; 
     for(String line: fileLine) { 
      if(line.contains(misspelled)) { 
       line.replaceAll(misspelled, replacement); 
       System.out.println("replaced word" + misspelled + "in line number" + i); 
       i++; 
      }else i++; 
     } 
    } 
+0

ありがとう!しかし何らかの理由で私はcorrectModeメソッドを完了して今交換するが、私はすべてのプログラムを実行すると、辞書名とtxtファイル名を要求して印刷するだけですtxtファイルを取り出してください。そうすれば、correctModeメソッドが実行されていなくても... –

+0

私はメソッドを完了しなかったことに気付きました。そのコメントを無視して、助けてくれてありがとう! –

関連する問題