它读取文件但是在将文件发送到新文件时没有正确打印?

时间:2017-10-20 04:36:23

标签: java io printwriter

在将读取文件打印到新文件时,我遇到了问题。所以这是财务文件中的示例数据:

资本

2215.281234

Weaver,Addison U。

902-6238 Purus,Avenue

兴趣

22343.623428

Frost,Tana Y。

P.O。 Enim Road,Box 902,3494

当我跑步时,我得到的只是:

名称:

地址:

因此,在“名称”或“地址”之后,它不会显示相应的名称或添加该税码。但是,它读取文件写入;问题是它不会在屏幕上或文件中打印名称和地址。如果有人能帮助我,我将不胜感激。打印是我遇到的唯一问题。提前谢谢。

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();
}
}

1 个答案:

答案 0 :(得分:3)

printf行错了。请改用以下行。

System.out.printf("Name: %s\nAddress: %s\n", name, address);

您的专线有什么问题,您将字符串"Name: ""Address: "作为参数传递给printf,以便它们用于替换2 %s