2017-10-20 8 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); 

은 2 %s을 대체하는 데 사용됩니다 있도록 printf에 매개 변수로 문자열 "Name: ""Address: "을 통과한다는 것입니다.

+0

정말 고마워요! 나는 약간만 조정해야하며 현재 작동 중입니다. 나는 이런 식으로 내 코드를 포맷하는 데 사용하고 이것은 처음으로 문제가 발생했습니다. –