2017-11-27 4 views
0

私の質問は、テキストファイル内の文字列を見つけて置き換えることです。 このプログラムはA Christmas Carolテキストファイル内の単語ghostのインスタンスを検索し、GhostGHOSTためBUNNYためghostためbunnyBunnyでそれらを交換することになっています。小文字のゴーストで動作し、GHOSTのインスタンスはありませんが、大文字のGhostだけでは動作しません。 私の問題は、最後の2つの方法の中にあることを知っています。私は間違って何をしていますか?あなたのsubstitute方法のif声明で再帰的な文字列の検索と置換?

package findandreplace; 

    import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.io.PrintWriter; 
    import java.util.ArrayList; 
    import java.util.Scanner; 


    public class FindAndReplace { 


    public static void main(String[] args) { 
     String filename = "24022-0.txt"; //Charles Dickens - A Christmas Carol 
     try { 
      Scanner fin = getScannerFromFile(filename); 
      String [] story = readFile(fin); 
      String [] newStory = new String[story.length]; 

      for (int i = 0; i < story.length; i++) { 
       newStory[i] = substitute(story[i], "ghost", "bunny"); 
      } 

      if (writeFile("bunny.txt", newStory)) 
       System.out.println("New file successfully created."); 

     } catch (FileNotFoundException e) { 
      System.out.println("File not found"); 
      System.exit(1); 
     } 
    } 

    private static Scanner getScannerFromFile(String filename) throws FileNotFoundException { 
     File f = new File(filename); 
     return new Scanner(f); 
    } 



    private static String [] readFile(Scanner fin) { 
     ArrayList<String> lines = new ArrayList<>(); 
     while (fin.hasNext()) { 
      lines.add(fin.nextLine()); 
     } 
     return lines.toArray(new String[0]); 
    } 


    private static boolean writeFile(String newfile, String [] lines) { 
     File file = new File(newfile); 
     if (file.exists()) { 
      System.out.println("File already exists"); 
     } 
     try { 
      PrintWriter fout = new PrintWriter(file); 
      for (String line : lines) { 
       fout.println(line); 
      } 
      fout.close(); 
     } catch (FileNotFoundException ex) { 
      System.err.println("File error."); 
      return false; 
     } 
     return true; 
    } 

    /** 
    * This method should examine the first word, and return the second word matching case. 
    * For example, if first word is "GHOST", and second word is "bunny", the return value 
    * should be "BUNNY". 
    * NOTE: THIS SHOULD *NOT* BE A RECURSIVE FUNCTION 
    * precondition: the first word is all lowercase, Capitalized, or ALL-CAPS 
    * @return second, matching case of first 
    */ 
    private static String matchWordCase(String first, String second) { 
     if (first.toUpperCase().equals(first)) { 
     second = second.toUpperCase(); 
     } 
     if (first.toLowerCase().equals(first)) { 
      second = second.toLowerCase(); 
     } 
     else if (first.toUpperCase().charAt(0) == first.charAt(0)) { 
      second = second.substring(0, 1).toUpperCase() + second.substring(1); 
     } 
     return second; 
    } 

    /** 
    * This method should return line, with every instance of the word find 
    * should be replaced by replace. There are multiple approaches you can take, 
    * but words should match regardless of case, and case should be preserved. 
    * If you want to assume words are space-separated, you may. However, it is 
    * better if you figure out how to do this otherwise. 
    * NOTE: THIS SHOULD BE A RECURSIVE FUNCTION 
    * @return line - with the appropriate substitutions 
    */ 
    private static String substitute(String line, String find, String replace) { 
     StringBuilder newLine = new StringBuilder(); 
     //TODO: 
     String result; 
     if (line.length() < find.length()) { 
      return line; 
     } 

     if (find.equals(line.substring(0,find.length()))) { 
     line = matchWordCase(find, replace) + 
     substitute(line.substring(find.length()), find, matchWordCase(find, 
     replace)); 
     } else { 
      result = line.charAt(0) + substitute(line.substring(1), find, 
     replace); 
     } 


     return result; 
    } 
} 
+0

私は私の考える代替方法でmatchWordCaseメソッドを呼び出そうとしていました。これは今私が持っているものであり、それはまだ大文字の大文字と一致していません。 –

答えて

0

、条件がfind.equalsIgnoreCase、代わりのfind.equalsすべきですか?

+0

「ゴースト」を「バニー」、「ゴースト」を「バニー」と出力する必要がある場合、「ゴースト」と「ゴースト」のすべてのインスタンスを「バニー」に出力しただけです。試してみる価値がある、ありがとう。 –

+0

私はあなたの質問の文句とあなたを混乱させたかもしれないと思います。私はトップでそれを編集しました。 –

+0

まあ、メソッドmatchWordCaseは、私が前提としているのは、コード内のどこでも呼び出されていないということです。 – adrianwong

関連する問題