它读取文件,但它将文件发送到新文件时没有正确打印?
问题描述:
将文件读取到新文件时,我遇到了问题。因此,这里的来自财务文件中的示例数据:它读取文件,但它将文件发送到新文件时没有正确打印?
资本
2215.281234
韦弗,艾迪U.
902-6238普鲁斯大道
利息
22343.623428
弗罗斯特,塔娜Y.
P.O.盒902,3494 Enim路
当我跑了,所有我得到的是:
名称:
地址:
等
打完了 “名”或“地址”,它不显示相应的名称或添加该税码。但是,它读取文件写入;它不会在屏幕上或文件中打印名称和地址。如果有人能帮助我,我将不胜感激。印刷是我唯一的问题。提前致谢。
package fh;
import java.util.Scanner; import java.io.*;
public class fh { public static void main(String [] args) throws IOException
{
String taxcode ,name , address;
double tax = 0, income = 0;
String financeAdd = "C:\\Users\\name\\workspace\\finance.txt";
String correctRec = "C:\\Users\\name\\workspace\\taxrecords.txt";
String wrongRec = "C:\\Users\\name\\workspace\\recorderror.txt";
File file = new File(financeAdd);
Scanner s = new Scanner(file);
PrintWriter outfile = new PrintWriter(correctRec);
PrintWriter outfile2 = new PrintWriter(wrongRec);
while(s.hasNext())
{
taxcode = s.nextLine();
switch (taxcode)
{
case "Dividend":
income = Double.parseDouble(s.nextLine());
name = s.nextLine();
address = s.nextLine();
tax = (income * 1.25 - (income * 1.25 * 0.33)) * 0.22;
outfile.printf("%s%n%s%n","Name: ","Address: ", name, address);
System.out.printf("%s\n%s\n","Name: ","Address: ", name, address);
break;
case "Interest":
income = Double.parseDouble(s.nextLine());
name = s.nextLine();
address = s.nextLine();
tax = income * 0.22;
outfile.printf("%s%n%s%n","Name: ","Address: ", name, address);
System.out.printf("%s\n%s\n","Name: ","Address: ", name, address);
break;
case "Capital":
income = Double.parseDouble(s.nextLine());
name = s.nextLine();
address = s.nextLine();
tax = income * 0.50 * 0.22;
outfile.printf("%s%n%s%n","Name: ","Address: ", name, address);
System.out.printf("%s\n%s\n","Name: ","Address: ", name, address);
break;
default:
income = Double.parseDouble(s.nextLine());
name = s.nextLine();
address = s.nextLine();
outfile2.printf("%s%n%s%n","Name: ","Address: ", name, address);
System.out.printf("%s\n%s\n","Name: ","Address: ", name, address);
break;
}
}
System.out.println("Data Processed");
s.close();
outfile.flush();
outfile.close();
outfile2.flush();
outfile2.close();
}
}
答
printf
行错了。请使用下面一行代替。
System.out.printf("Name: %s\nAddress: %s\n", name, address);
有什么问题你行,你把这个字符串"Name: "
和"Address: "
作为参数printf
,使它们被用来取代2 %s
。
非常感谢!我只需稍微调整一下,现在就开始工作了。我曾经这样格式化我的代码,这是我第一次遇到问题。 –