2017-10-20 60 views
-1

將文件讀取到新文件時,我遇到了問題。因此,這裏的來自財務文件中的示例數據:它讀取文件,但它將文件發送到新文件時沒有正確打印?

資本

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

回答

3

printf行錯了。請使用下面一行代替。

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

有什麼問題你行,你把這個字符串"Name: ""Address: "作爲參數printf,使它們被用來取代2 %s

+0

非常感謝!我只需稍微調整一下,現在就開始工作了。我曾經這樣格式化我的代碼,這是我第一次遇到問題。 –

相關問題