在java textArea中复制和粘贴导致问题

问题描述 投票:0回答:1

我正在使用textArea来收集用户的信息。用户必须每行输入一条信息。要记录此信息,我使用的是字符串数组。当我测试它时,如果我手动键入行,它会工作,但如果我复制并粘贴行的信息并删除空格,我会得到一个NumberFormatException。

//from applet class
private void record(java.awt.event.ActionEvent evt) {

    Test.copy(display.getText());

}

课堂考试

public class Test{

    public Test() {

    }

    public void record() {    
        String [] lines = new String [4];

        lines = str.split("\n");

        String workerName = lines[0];
        String workerDepartment = lines[1];
        String workerID = lines[2];
        String workerPhone = lines[3];


        int wID = Integer.parseInt(workerID);
        int wPhone = Integer.parseInt(workerPhone);
    }
}

这有效......

约翰

老板

10

5555555555

事实并非如此

约翰老板10 5555555555

(在boss之前点击,按回车,在10之前点击,按回车等)

java textarea whitespace
1个回答
0
投票

我认为这是因为你只是在新线上分裂。尝试替换这个:

lines = str.split("\n");

有了这个:

lines = str.split("\\s+");
© www.soinside.com 2019 - 2024. All rights reserved.