Exception
in
thread
"
main"
java.util.IllegalFormatConversionException: d != java.lang.
String
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:
4302
)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:
2793
)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:
2747
)
at java.util.Formatter.format(Formatter.java:
2520
)
at java.io.PrintStream.format(PrintStream.java:
970
)
at java.io.PrintStream.printf(PrintStream.java:
871
)
at practical3.main(practical3.java:
34
)
What I have tried:
* @(#)P3Q1S.java
* @author
* @version 1.00 2020/7/3
import java.util.Scanner;
public
class
practical3 {
public
static
void
main (String[] args) {
Scanner scanner =
new
Scanner(System.
in
);
int[] Denomination = {
100
,
50
,
20
,
10
,
5
,
1
};
int[] Quantity =
new
int
[Denomination.length];
System.
out
.printf(
"
\n\n%20s %20s\n"
,
"
Denomination (RM)"
,
"
Quantity"
);
for
(
int
i=
0
;i<Denomination.length;i++)
System.
out
.printf(
"
\n %20s\t\t\t\t\t"
,Denomination[i]);
Quantity[i]=scanner.nextInt();
int
subtotal=
0
,total=
0
;
System.
out
.printf(
"
\n\n%20s %20s %20s\n"
,
"
Denomination (RM)"
,
"
Quantity"
,
"
Value (RM)"
);
for
(
int
i=
0
;i<Denomination.length;i++ )
subtotal= Denomination[i] * Quantity[i];
System.
out
.printf(
"
\n\n%20d %20d %20d\n"
,
"
Denomination[i]"
,
"
qty[i]"
,
"
subtotal"
);
total+=subtotal;
System.
out
.println(
"
Grand total(RM)="
+total);
You are passing strings to the print formatter where your format string requires integers:
System.out.printf(
"
\n\n%20d %20d %20d\n"
,
"
Denomination[i]"
,
"
qty[i]"
,
"
subtotal"
);
System.out.printf(
"
\n\n%20d %20d %20d\n"
, Denomination[i], qty[i], subtotal);
Why the quotes around Denomination[i] and qty[i] and subtotal in this
System.
out
.printf(
"
\n\n%20d %20d %20d\n"
,
"
Denomination[i]
"
,
"
qty[i]
"
,
"
subtotal
"
); Shouldn't it be
System.
out
.printf(
"
\n\n%20d %20d %20d\n"
,
Denomination[i]
,
qty[i]
,
subtotal
); ??
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
public class P3Q1S {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
int[] Denomination = {100,50,20,10,5,1};
int[] Quantity = new int [Denomination.length];
System.out.printf("\n\n%20s %20s\n","Denomination (RM)","Quantity");
for (int i=0;i