如何停止 getLineCount();从计数空字符串变量“\n”

How to stop getLineCount(); from counting empty string varianle "\n"

我正在尝试创建一个文本区域来计算选项窗格中的行数或键入的文本。也许是一个简单的问题,但我似乎无法阻止 linecounter 计算空行。

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

如果您始终恰好附加一个换行符,您可以简单地减去 1:

linecount = textArea.getLineCount() - 1;