0

I am building a program which allows you to upload a text file and then search for words which form different words when spelt backwards and forwards. This is what I have so far:

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


}



}

My question is how would you find if another word was in the file which was a different word backwards.

E.g. if the file had the word 'saw', how would I find if there was a separate word 'was' which is 'saw' backwards?

Thanks

pcbf
  • 11
  • 3
  • 1
    Not exactly. A palindrome is a word that is spelled the same backwards and forwards. – Skam Jul 02 '17 at 18:29
  • I suggest that you learn how to break your code into separate methods and how to use local variables. There is little reason to declare all of these variables as fields. – Code-Apprentice Jul 02 '17 at 18:32

2 Answers2

0

Make additional string with whole text reversed, you can see here how to do that: Reverse a string in Java

Than split words from original string, like this: Split string into individual words Java

And than I would search for splitted words in reversed string: How to find a whole word in a String in java

0

My question is how would you find if another word was in the file which was a different word backwards. E.g. if the file had the word 'saw', how would I find if there was a separate word 'was' which is 'saw' backwards?

You could follow this algorithm:

  • Create an empty set of strings, to put words into it
  • For each word in the text
    • Check if the reversed word is in the set, and act on the result accordingly
    • Add the word to the set

Watch out for uppercase/lowercase letters. You might want to lowercase all words as you iterate over the input and store words in the set.

janos
  • 120,954
  • 29
  • 226
  • 236