我正在制作一个简单的教程程序,其中用户输入一些数据,然后将该数据写入文本文件,然后读取该文本文件并显示数据。但是,并非所有数据都被写入文本文件。请查看以下内容:

这是我的代码:

package chasi.fecalMatter.ExcrementalProgram;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JOptionPane;

public class RedundantExcrement {

    static String inputName = JOptionPane.showInputDialog("Please input your FULL name:");
    static String inputUsername = JOptionPane.showInputDialog("Please input your desired Username:");
    static String inputJobTitle = JOptionPane.showInputDialog("Please input your Job title:");
    static String inputSalary = JOptionPane.showInputDialog("Please input your monthly salary:");

    static int convertedSalary = 0; /*Convert salary from String to int*/
    static int benefitDeduction = 0;

    static String empFileName = inputUsername+"INFO";
    static BufferedWriter bwriter = null;

    public static void main (String[] args) throws IOException {
        //Catch NumberFormatException.
        try {
            Integer.parseInt(inputSalary);
        } catch (NumberFormatException nfEx) {
            JOptionPane.showMessageDialog(null, "Please ensure that " +
                    "your salary is entered in NUMERIC form.", "ERROR!", 2);
            return;
        }

        //Specify instructions as regard salary -to- Benefit deduction ratio
        if (convertedSalary >= 3500) {
            benefitDeduction = 300;
        } else {
            benefitDeduction = 180;
        }

    try { /*Catches IOException AND NullPointerException*/
            FileWriter fwriter = new FileWriter(empFileName);
            bwriter = new BufferedWriter(fwriter);

            bwriter.write(inputName);
            bwriter.newLine();
            bwriter.write(inputJobTitle);
            bwriter.newLine();
            bwriter.write(inputSalary);
            bwriter.newLine();
            bwriter.write(benefitDeduction);
            bwriter.newLine();
            bwriter.write("----------");
            bwriter.newLine();
        } catch (IOException | NullPointerException ionpEx) {
            JOptionPane.showMessageDialog(null, "An ERROR has occured", "ERROR!", 2);
            return;
        } finally { /*CLOSE the writer*/
            try{
                if (bwriter != null) {
                    bwriter.close();
                }
            } catch (IOException ioEx2) {
                JOptionPane.showMessageDialog(null, "ERROR!");
                return;
            }
        }
    }
}


如您所见,我使用int convertedSalary将用户输入的inputSalary从字符串转换为数字。在我的方法中,我使用

try {
    Integer.parseInt(inputSalary);
} catch (NumberFormatException nfEx) {
    JOptionPane.showMessageDialog(null, "Please ensure that " +
            "your salary is entered in NUMERIC form.", "ERROR!", 2);
    return;
}


为了隐蔽它。稍后(应该)由BufferedWriter编写。

我也有这段代码:

try {
    Integer.parseInt(inputSalary);
} catch (NumberFormatException nfEx) {
    JOptionPane.showMessageDialog(null, "Please ensure that " +
                "your salary is entered in NUMERIC form.", "ERROR!", 2);
    return;
}


为了根据用户薪水的值更改我的benefitDeduction的值。

但是,benefitDeduction被写入文本文件,但是我得到了:

谢谢您的帮助!

最佳答案

查看API

public void write(int c)
   throws IOException



  写一个字符。要写入的字符包含在
  给定整数值的16个低位; 16高阶
  位被忽略。


这意味着int实际上是一个字符,将根据Unicode / ASCII表进行转换。

要写整数值,请使用bwriter.writer(String.valueOf(benefitDeduction));

07-24 09:34