【问题标题】:It reads the file but it is not printing it properly when it sends it to a new file?它读取文件但将其发送到新文件时无法正确打印?
【发布时间】:2017-10-20 04:36:23
【问题描述】:

在将读取的文件打印到新文件时遇到问题。下面是财务文件中的示例数据:

资本

2215.281234

韦弗,艾迪生大学

902-6238 Purus, Avenue

兴趣

22343.623428

弗罗斯特,塔娜 Y。

邮政信箱埃尼姆路 3494 号 902 号信箱

当我运行它时,我得到的只是:

姓名:

地址:

等等

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

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

【问题讨论】:

    标签: java io printwriter


    【解决方案1】:

    printf 行是错误的。请改用以下行。

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

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

    【讨论】:

    • 非常感谢!我只需要稍微调整一下,它现在就可以工作了。我曾经像这样格式化我的代码,这是我第一次遇到问题。
    猜你喜欢
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多