Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);
    }
    
    
}
Posted
Updated 20-Aug-20 1:59am
Comments
Member 14918856 20-Aug-20 10:55am    
thanks a lot i have solve the problem

You are passing strings to the print formatter where your format string requires integers:
Java
System.out.printf("\n\n%20d %20d %20d\n","Denomination[i]","qty[i]","subtotal");

// the above sholud be:
System.out.printf("\n\n%20d %20d %20d\n", Denomination[i], qty[i], subtotal); // remove quotes from the variable names. Also, what is "qty", you do not have a variable by that name.
 
Share this answer
 
Comments
Member 14918856 20-Aug-20 10:52am    
import java.util.Scanner;

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
Member 14918856 20-Aug-20 10:55am    
thanks a lot i have solve the problem
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);
??
 
Share this answer
 
Comments
Member 14918856 20-Aug-20 10:51am    
same error occur when after i update the code
Member 14918856 20-Aug-20 10:55am    
thanks a lot i have solve the problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900