2015-01-29 5 views
0

나는 마지막 줄에 오류가 있다고 확신한다. 그리고 내가 말할 수있는 것부터, "% d"변수를 사용하는 것을 기쁘게 생각하지 않는다. 그러나 정수에 대해 유효한 입력이 아닌가?내 코드에서 "잘못된 형식 변환 예외"오류가 발생하는 이유를 모르십니까?

import java.util.Scanner; 

public class total_cost { 
    public static void main(String[] args) { 

     int TVs; 
     int VCRs; 
     int controller; 
     int CD; 
     int recorder; 
     double TV_price; 
     double VCR_price; 
     double controller_price; 
     double CD_price; 
     double recorder_price; 
     double tax; 
     { 
      TV_price = 400.00; 
      VCR_price = 220.00; 
      controller_price = 35.20; 
      CD_price = 300.00; 
      recorder_price = 150.00; 
      tax = .0825; 

      Scanner in = new Scanner(System.in); 
      { 
       System.out.printf("How many TV's were sold? "); 
       TVs = in.nextInt(); 

       System.out.printf("How many VCR's were sold? "); 
       VCRs = in.nextInt(); 

       System.out.printf("How many remote controller's were sold? "); 
       controller = in.nextInt(); 

       System.out.printf("How many CD players were sold? "); 
       CD = in.nextInt(); 

       System.out.printf("How many Tape Recorder's were sold? "); 
       recorder = in.nextInt(); 

       System.out.printf("QTY\tDESCRIPTION\tUNIT PRICE\tTOTAL PRICE\n"); 
       System.out.printf("%d", TVs + "\tTelevision\t%f", TV_price 
         + "\t" + tax * TV_price + "%f", TV_price); 
      } 
     } 
    } 
} 

오류 메시지 :

System.out.println(3 + "\tTelevision\t" + TV_price + "\t" + tax * TV_price + " " + TV_price); 

printf() 먼저 넣어 함께 : 당신에 훨씬 더 적합한 것으로 printf() 또는 보인다 println()을 사용하려는 경우

Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String 
    at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4011) 
    at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2725) 
    at java.util.Formatter$FormatSpecifier.print(Formatter.java:2677) 
    at java.util.Formatter.format(Formatter.java:2449) 
    at java.util.Formatter.format(Formatter.java:2383) 
    at java.lang.String.format(String.java:2781) 
    at total_cost.main(total_cost.java:37) 
+0

마지막'printf' 호출은 '% d'인 단일 자리 표시 자로 구성된 형식 문자열을 갖지만 첫 번째 형식 인수는 '% d'에 의해 예상되는 숫자 대신 문자열입니다. –

+1

코드를 들여 씁니다. 또한 전체 코드를 게시하지 말고 문제를 재현 할 수있는 최소한의 컴파일 가능한 코드 예제를 만듭니다. stacktrace와 같은 문제 자체에 대한 자세한 내용을 추가하십시오. – Pshemo

+1

무엇? 변수 TV는 int로 선언됩니다. – Andrea

답변

0

당신은 결정해야 형식 문자열은 전체적으로 다음과 같은 모든 값을 갖습니다.

System.out.printf("%d number %s string\n", 3, "hi"); 
+0

: "printStream 형식의 메서드 println (String)은 인수 (String, String, String, double)에 적용 할 수 없습니다." 또한 "String.out을 확인할 수 없습니다." – Andrea

+0

입력 한 내용을 읽어주십시오. 아니면 적어도 붙여 넣기 등을 복사하십시오.'println()'함수에 ONE 및 ONE 인수 만 전달하십시오. 모든 것을 함께 추가하십시오. –

3

정수가 아닌 문자열을 전달합니다. 그것은 다음과 같아야합니다 :

System.out.printf("%d\tTelevision\t%.2f\t%.2f,%.2f", TVs, TV_price,tax * TV_price, TV_price); 

추신 : 나는 소수 자릿수 2 개로 가격을 형식화 할 자유를 가졌습니다.

+0

여전히 string.out을 해결할 수 없다고 말합니다. – Andrea

+0

무엇입니까? 'System.out'을 의미합니까? 대문자 'S'를 사용하십시오. – Axel

0

당신 때문에이 라인의 예외를 받고있어 다음 TVs + "\tTelevision\t%f" 때문에 +의 산술 연산을 수행하려고

System.out.printf("%d", TVs + "\tTelevision\t%f", TV_price + "\t" + tax * TV_price + "%f", TV_price);

.

출력을 포맷 한 다음 값을 제공해야합니다.

예 : System.out.printf("%d %s", TVs, "\tTelevision\");

0

내가 특별히 정확하게 자바에 익숙한 아니지만, 거의 내가 아는 모든 "출력 형식"함수는 먼저 형식 문자열과 패턴을 따라하고, 나중에 모든 인수. 그래서

printf("This is a string %d %d %s %f", arg1, arg2, arg3, arg4); 

당신이 보이는 무엇을하려고 될 같은

동시에 형식 문자열과 정상 연결 둘 다 할 것입니다.

관련 문제