Ad

How To Stop GetLineCount(); From Counting Empty String Varianle "\n"

- 1 answer

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!");
            }
        });```
Ad

Answer

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

linecount = textArea.getLineCount() - 1;
Ad
source: stackoverflow.com
Ad