Search code examples
javaswingjtextarea

How can I write the text from JTextArea to a file except certain words stored as strings?


Basically, I built an interface that takes a user's input into a text area. After pressing Control + C the user saves the input to a file.txt.

The interface has two texts by default: one shows when the program starts ("Please enter your text in here!") and the ther when the user successfully saved the text after pressing Control + C keys ("Text added successfully!").

The problem is by using Control + C keys users can save even those two default texts that I mentioned above. I am using keyListener to achieve my goal and it works just fine. Also, I wrote an "If" statement in order to exclude those two default texts, but it doesn't work.

txtArea.addKeyListener(new KeyListener() {

    @Override
    public void keyTyped(KeyEvent e) {
        }
    @Override
    public void keyPressed(KeyEvent e) {
            String notThisTxt = txtArea.getText();
            String text1 = "Please enter your text in here!";
            String text2 = "Text added successfully!";

            if(!notThisTxt.contentEquals(text1) || !notThisTxt.contentEquals(text2)) {
            if ((e.getKeyCode() == KeyEvent.VK_C) && e.isControlDown()) {
                try {
                    file.createNewFile();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                    FileWriter fw = null;
                    String someSpace = " ";
                    BufferedWriter br = null;
            try {
                    fw = new FileWriter(file.getAbsoluteFile(), true);
                    br = new BufferedWriter(fw);

                    br.write(someSpace);
                    txtArea.write(br);  
                }catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }finally {
                }try {
                        br.close();
                        fw.close();
                }catch(IOException e2) {
                        System.out.println("The programm doesn't want to close!" + e2);
                    }
                }
                    txtArea.setText(txtAdded);
                }
                else{
                       System.out.println("test");
                    }      
                }

            @Override
            public void keyReleased(KeyEvent e) {
            }
        });

I expected from this code to get all the text a user can possibly enter and save it a file.txt, except the 2 strings containing default texts.


Solution

  • if (!notThisTxt.contentEquals(text1) || !notThisTxt.contentEquals(text2)) will always evaluate to true, because it is impossible for a string to be equal to two different strings at the same time. notThisTxt is guaranteed not to be equal to at least one of those.

    You probably meant to check whether notThisTxt ≠ text1 and notThisTxt ≠ text2.

    You want to write your code to exactly do that above sentence. Specifically, you need to use ‘and’ (&&) instead of ‘or’ (||).