Home > Software engineering >  How to stop getLineCount(); from counting empty string varianle "\n"
How to stop getLineCount(); from counting empty string varianle "\n"

Time:02-17

I'm trying to make a text area that will count the lines or typed text from an Option pane. Maybe a simple problem but I can't seem to prevent the linecounter from counting the empty line.

        btnEntertext.addActionListener(new ActionListener() {
            private int linecount;
            public void actionPerformed(ActionEvent e) {
                String text = JOptionPane.showInputDialog("Enter Text:");
                textArea.append(text   "\n");
                linecount = textArea.getLineCount();
                String amount = new String (String.valueOf(linecount));
                lblnumlines.setText("Number of lines:"   " "   amount   " "   "lines!");
            }
        });```

CodePudding user response:

If you always append exactly one newline character, you could simply subtract by 1:

linecount = textArea.getLineCount() - 1;
  • Related