2017-07-02 58 views
0

我正在構建一個程序,允許您上傳文本文件,然後搜索單詞形成不同的單詞時拼寫向後和向前。這是我到目前爲止有:文本搜索詞向後拼寫時包含另一個詞

public class RWF extends Application { 

private Window mainStage; 
private FileChooser fileChooser; 
private String path; 
private File file; 
public String x; 
public TextArea textArea; 
public Iterator m; 
public String reversed; 
public ArrayList al; 
public String[] words1; 
public String z; 
public Object element; 

public String text; 
private String[] pa; 
private String sa; 
private Array palindromeArray; 
private String longest; 
public Button openButton2; 
private StringBuilder stringBuilder; 
private BufferedReader bufferedReader; 
public StringBuilder sb; 
private BufferedReader br; 
private String[] xsplit; 
private String[] xsplit2; 
private String words; 
private String word; 
public String str; 
public String str2; 


public RWF(){ 

    sb = stringBuilder; 
    str2 = "Hellow"; 


} 


public void start(Stage stage) { 

    TextArea textArea = new TextArea(); 
    textArea.setPrefColumnCount(100); 
    textArea.setPrefRowCount(100); 

    VBox vbox = new VBox(textArea); 

    final Button openButton = new Button("Open a Text File"); 
    final Button openButton2 = new Button("Analyse Text File"); 
    Group root = new Group(openButton, openButton2, textArea); 
    Scene scene = new Scene(root, 400, 300); 

    FileChooser fileChooser = new FileChooser(); 
    fileChooser.setTitle("Open Resource File"); 
    fileChooser.getExtensionFilters().addAll(
      new ExtensionFilter("Text Files", "*.txt"), 
      new ExtensionFilter("All Files", "*.*")); 



    openButton.setLayoutX(250); 
    openButton.setLayoutY(220); 
    openButton2.setLayoutX(150); 
    openButton2.setLayoutY(120); 
    textArea.setLayoutX(450); 
    textArea.setLayoutY(320); 
     stage.setTitle("Text Analyser"); 
     stage.setScene(scene); 
     stage.show(); 

     openButton.setOnAction(
       new EventHandler<ActionEvent>() { 


       @Override 
        public void handle(final ActionEvent e) { 
         File file = fileChooser.showOpenDialog(stage); 
         if (file != null) { 
          try { 
          textArea.setText(readFile(file)); 
         } catch (IOException e1) { 
          // TODO Auto-generated catch block 
          e1.printStackTrace(); 
         } 
         } 


       } 



       private String readFile(File file) throws IOException { 



StringBuilder sb1 = new StringBuilder(); 
       bufferedReader = new BufferedReader(new FileReader(file)); 


        text = bufferedReader.readLine(); 
        sb1.append(text); 

        System.out.println(sb1); 





        String u = sb1.toString(); 
        xsplit2 = u.split(u); 
        for (String words : xsplit2){ 
         System.out.println(words); 
        } 

        StringBuilder t = sb1.reverse();  
       String x = t.toString(); 
      xsplit = x.split(x); 
      for (String word : xsplit){ 
       System.out.println(word); 
      } 

      for (int i=0; i<xsplit2.length; i++){ 
       boolean found = false; 
       for (int j=0; j<xsplit.length; j++){ 
        if ((xsplit2[i]).equals(xsplit[j])){ 
      found = true; 




      str = Arrays.toString(xsplit); 
      str2 = Arrays.toString(xsplit2); 


       java.nio.file.Path path = Paths.get("Users/paulc/workspace5000/words.txt"); 
       byte[] readBytes = Files.readAllBytes(path); 
       String wordListContents = new String(readBytes, "UTF-8"); 
       String[] words = wordListContents.split("\n"); 
       Set wordsSet = new HashSet<>(); 
       Collections.addAll(wordsSet, words); 

       if (wordsSet.contains(str) && wordsSet.contains(str2)){ 
        textArea.appendText(str); 
       } 


openButton2.setOnAction(
      new EventHandler<ActionEvent>() { 






      @Override 
       public void handle(final ActionEvent e) { 
       textArea.appendText(str2); 
        } 

}); 
     } 
       } 
      } 
      return str2; 
       }; 
       }); 
} 


public static void main(String[] args){ 
    Application.launch(args); 


} 



} 

我的問題是如何將你會發現,如果另一個詞是這是一個不同的字倒着的文件中。

E.g.如果文件中有'鋸'這個詞,我怎麼會發現是否有一個單詞'was',這個單詞是'向後看'的?

感謝

+1

不完全。迴文是一個前後相同拼寫的單詞。 – Skam

+0

我建議你學習如何將代碼分解成單獨的方法以及如何使用局部變量。沒有理由將所有這些變量聲明爲字段。 –

回答

0

我的問題是,如果有另一個單詞在該文件是一個不同的詞向後。 例如如果文件中有'鋸'這個詞,我怎麼會發現是否有一個單詞'was',這個單詞是'向後看'的?

你可以按照這個算法:

  • 創建一個空字符串集,把話進去
  • 對於每一個字的文字
    • 檢查反轉字並據此採取行動
    • 將字添加到集

小心大寫/小寫字母。 當您迭代輸入並將單詞存儲在集合中時,您可能希望小寫所有單詞。

相關問題