读长之间的空间

时间:2014-06-02 03:19:39

标签: java swing spaces

这里的问题在于JButton" AddEmployee"。

长期的电话号码和社会保障I.D.不要在文本文件中显示空格。

JButton btnAddEmployee = new JButton("Add Employee");
        btnAddEmployee.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0){
                try{
                    String name = txtName.getText();
                    String address = txtAddress.getText();
                    double pay = Double.parseDouble(txtPay.getText());
                    long ssid = Long.parseLong(txtSSID.getText());
                    long number = Long.parseLong(txtNumber.getText());
                    FinalProjectGroup1 ee = new FinalProjectGroup1(name, address, pay, ssid, number);
                    FileWriter writer = new FileWriter(txtFolder.getText(), true);
                    PrintWriter pw = new PrintWriter(writer);
                    pw.println(ee.getName() + ", " + ee.getAddress() + ", " + ee.getNumber()
                    + ", " + ee.getPay() + ", " + ee.getSSID());
                    pw.close();
                }
                catch (Exception e){
                    return;
                }
            }
        });

1 个答案:

答案 0 :(得分:3)

我看到的一个主要问题是,您正在尝试处理电话号码和社会安全号码,就好像它们是数字一样,多为准确,事实上它们并非如此。虽然它们由数字字符组成,但您永远不会考虑对它们执行数字运算,例如加法,减法或乘法。始终将它们作为字符串处理。


修改
此外,正如MadProgrammer警告的那样,永远不要忽略您在此处所做的异常:

try
{
    String name = txtName.getText();
    String address = txtAddress.getText();
    double pay = Double.parseDouble(txtPay.getText());
    long ssid = Long.parseLong(txtSSID.getText());
    long number = Long.parseLong(txtNumber.getText());
    FinalProjectGroup1 ee = new FinalProjectGroup1(name, address, pay, ssid, number);
    FileWriter writer = new FileWriter(txtFolder.getText(), true);
    PrintWriter pw = new PrintWriter(writer);
    pw.println(ee.getName() + ", " + ee.getAddress() + ", " + ee.getNumber()
    + ", " + ee.getPay() + ", " + ee.getSSID());
    pw.close();
}

catch (Exception e)
{
    return;
}

至少,打印堆栈跟踪,以便您知道可能发生的坏事:

try
{
    String name = txtName.getText();
    String address = txtAddress.getText();
    double pay = Double.parseDouble(txtPay.getText());
    long ssid = Long.parseLong(txtSSID.getText());
    long number = Long.parseLong(txtNumber.getText());
    FinalProjectGroup1 ee = new FinalProjectGroup1(name, address, pay, ssid, number);
    FileWriter writer = new FileWriter(txtFolder.getText(), true);
    PrintWriter pw = new PrintWriter(writer);
    pw.println(ee.getName() + ", " + ee.getAddress() + ", " + ee.getNumber()
    + ", " + ee.getPay() + ", " + ee.getSSID());
    pw.close();
}

catch (Exception e)
{
    e.printStackTrace();
}