2017-12-09 10 views
0

辞書とtxtファイルを取り、txtファイルの単語が辞書にない場合は、誤った単語を変更する機会がユーザに与えられます。修正が行われると、修正されたtxtファイルが出力されます。ここで正しく置き換えられない単語

は、ファイルtxtの例です:たとえば

twinkx twinkx lottle star how I 
lottle lamb its a warld of laughter a 
warld of tears 

私はtwinkxを修正したい場合、それは私に尋ねるだろう、と私はそれがきらきらして切り替えると、それは上記のように出力と同じことまだだろう、とそれはまだtwinkxと言っています。それは、私の交換方法に何か問題があると私に信じさせる。それのためのアルゴリズムはコメントに記述されているので、私は何かを逃した可能性があります。

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


private void correctionMode(){ 
    //for each line in fileLines: 
    // for each word in the line: 
    //  if the word is in wrongWords: 
    //   display the word and ALL the line numbers it appears on 
    //   ask user if they like to replace?(y/n) 
    //   if user chooses y: 
    //    ask for a new word and call the replace method replaceAll occurrences 
    //   for either y or n delete word from wrongWords  
    //Hint: for parsing lines look back at the processFile() 
    for(String line: fileLine) 
    for(String w: line.split("\\s")) 
     if(wrongWords.containsKey(w)){ 
      System.out.println(w + " " + 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); 
      } 
      else if(r.equals("n")){ 
       wrongWords.remove(w); 
      }    
     }       
} 

答えて

0

ラインはループ変数だけです。

for (int i = 0; i < fileLine.size(); i++) { 
     fileLine.set(i, fileLine.get(i).replace(misspelled, replacement)); 
    } 
+0

あなた/サーさんは救助者です!どうもありがとうございます!!!!!! –

0
line.replace(a, b) 

は、既存のラインオブジェクトを変更しません。ここで

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); 
     for(String line : fileLine){ 
      line.replace(misspelled, replacement); 
     } 
    } 

は、最初の場所で置き換えを呼び出す方法です。代わりに、置き換えられたシンボルを持つ新しいオブジェクトを返します。だからあなたはその行を

に変更する必要があります
line = line.replace(misspelled, replacement); 
+0

でも、最後に出力が変わることはありません。それでも変わりません。 –

関連する問題