Search code examples
javaif-statementtextareakeypresskeylistener

create if statement on each individual line of a textarea object


My java code is trying to create a if statement based on if the individual line of the text area object is filled or not. My code works when there is nothining in the text area box however as soon as a character is entered the if text is empty does not work anymore.As you can see in the gif. When a character is entered and the user hits enter even though the line has nothing on it. Sam is still added to the line. That should not happen. Sam should be only added to the line if character is on the line.

enter image description here

 class text11 extends JFrame implements ActionListener{ 

static JFrame f; 




static JTextArea jt; 

// main class 
public static void main(String[] args) 
{ 
    // create a new frame to store text field and button 
    f = new JFrame("textfield"); 



    jt.addKeyListener(new CustomKeyListener());


}




}

public void keyPressed(KeyEvent e) {
   if(e.getKeyCode() == KeyEvent.VK_ENTER){

       if(text11.jt.getText().trim().isEmpty()) {


           System.out.println( "Hi" );


       }
       else {
           text11.jt.setText(text11.jt.getText() + "     sam");

       }



   }
}

Solution

  • .getText() returns all of the text. Not just 'the last line entered'. You can for example use string's split method to first break up the text into individual lines, and then check the last line.

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            // Split the entire text string into lines.
            String[] lines = text11.jt.getText().split("\r?\n");
    
            // Zero lines _OR_ the last line, after trimming, is empty?
            if (lines.length == 0 || lines[lines.length - 1].trim().isEmpty()) {
                System.out.println( "Hi" );
           } else {
               text11.jt.setText(text11.jt.getText() + "     sam");
           }
        }
    }
    

    alternatively, you could start at the end and walk back, checking each character. More code, but theoretically faster (but it won't be noticable until you put in a lot of text, on the order of a megabyte's worth or more. I'd go with the one you find easiest to follow:

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            String txt = text11.jt.getText();
            boolean hasText = false;
    
            // Walk from last character to first.
            for (int i = txt.length() - 1; i >= 0; i--) {
                // Is it a newline? Then we're done; there is no text on last line.
                if (txt.charAt(i) == '\n') break;
                // Is it whitespace (spaces or tabs)? Keep looking.
                if (Character.isWhitespace(txt.charAt(i))) continue;
                // There is text here. Mark that down and we're done.
                hasText = true;
                break;
            }
    
            if (hasText) {
               text11.jt.setText(text11.jt.getText() + "     sam");
           } else {
                System.out.println( "Hi" );
           }
        }
    }
    

    This last one will walk backwards, continuing if it sees whitespace, stopping if it sees non-whitespace characters (with the result: There are actual characters on the last line), and also stopping if it sees a newline character (with the result: There are no actual characters on the last line).