它读取文件但在将文件发送到新文件时没有正确打印?
It reads the file but it is not printing it properly when it sends it to a new file?
我在将读取的文件打印到新文件时遇到了问题。下面是财务文件中的示例数据:
首都
2215.281234
Weaver, Addison U.
902-6238 普鲁斯大街
兴趣
22343.623428
弗罗斯特,塔娜 Y
P.O。恩宁路 3494 号 902 号信箱
当我 运行 它时,我得到的是:
姓名:
地址:
等等
因此在 "Name" 或 "Address" 之后它不会显示相应的名称或添加该税码。但是,它读取文件写入;它没有在屏幕上或文件中打印名称和地址的问题。如果有人可以帮助我,我将不胜感激。打印是我遇到的唯一问题。提前致谢。
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
。
我在将读取的文件打印到新文件时遇到了问题。下面是财务文件中的示例数据:
首都
2215.281234
Weaver, Addison U.
902-6238 普鲁斯大街
兴趣
22343.623428
弗罗斯特,塔娜 Y
P.O。恩宁路 3494 号 902 号信箱
当我 运行 它时,我得到的是:
姓名:
地址:
等等
因此在 "Name" 或 "Address" 之后它不会显示相应的名称或添加该税码。但是,它读取文件写入;它没有在屏幕上或文件中打印名称和地址的问题。如果有人可以帮助我,我将不胜感激。打印是我遇到的唯一问题。提前致谢。
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
。