【发布时间】:2015-01-07 19:11:10
【问题描述】:
我正在制作一个简单的教程程序,其中用户输入一些数据,然后将数据写入文本文件,然后读取文本文件并显示数据。但是,并非所有数据都写入文本文件。请查看以下内容:
这是我的代码:
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 被写入文本文件,但我得到了这个:
如果您能提供帮助,谢谢!
【问题讨论】:
-
I also have this code这是复制和粘贴错误吗?它包含与上面相同的代码。 -
你用的是哪个jdk?标准 java 中没有
BufferedWriter.write(String)方法。你的代码真的可以编译吗? -
顺便说一句:似乎
convertedSalary将始终为0,因为您没有将Integer.parseInt(inputSalary);的返回值分配给它。并且没有变量userSalary,但inputSalary似乎被写入文件,因为它包含2500作为第三行(即bwriter.write(inputSalary);)。 -
@Dima 当然有。继承自Writer
-
@Dima jdk7。是的,它确实编译
标签: java printwriter bufferedwriter writer