Exception in thread "main" java.util.IllegalFormatConversion

Please Help Run Errors

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 5183 Views - Last Post: 22 July 2008 - 08:09 PM Rate Topic: -----

#1 UofPgirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-July 08

Exception in thread "main" java.util.IllegalFormatConversion

Posted 21 July 2008 - 02:40 PM

I am trying to get a Inventory Program to run, I have several errors and i do not understand what they mean.
List of Errors:
init:
deps-jar:
Compiling 1 source file to C:\NetBeansProjects\SCHOOL1\JavaApplication27\build\classes
compile-single:
run-single:
Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:3992)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2708)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2660)
at java.util.Formatter.format(Formatter.java:2432)
at java.util.Formatter.format(Formatter.java:2366)
at java.lang.String.format(String.java:2770)
at javaapplication27.DVD.toString(FGInventory2.java:132)
at java.lang.String.valueOf(String.java:2827)
at java.io.PrintStream.println(PrintStream.java:771)
at javaapplication27.FGInventory2.<init>(FGInventory2.java:21)
at javaapplication27.FGInventory2.main(FGInventory2.java:33)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

My Code:
package javaapplication27;





public class Inventory2{
	  
	  
		Inventory2() {	// constructor  
			  
		   DVD[] dvd =  new DVD[4];  
	  
			dvd[0] = new DVD(685.0,"How to Loose a Guy in 10 days", 4, 19.99 );  
			dvd[1] = new DVD(565.0,"Cyote Ugly", 8, 19.98 );  
			dvd[2] = new DVD(785.0,"Legally Blonde", 10, 19.99);  
			dvd[3] = new DVD(578.0,"Sweet Home Alabama",8,18.56); 
		   
			  for(int i = 0; i < 4; i++) 
			  {  
			   System.out.println(dvd[i]);  
			   System.out.println("The item number is " + dvd[i].getdvdItemNumber());
			   System.out.println("Product Title is " + dvd[i].getdvdTitle());  
			   System.out.println("The number of units in stock is" + dvd[i].getdvdStock());  
			   System.out.println("The price of each DVD is" + dvd[i].getdvdPrice());
			   System.out.println("The value of the inventory is" + dvd[i].Calvalue());  
		   }  
	 
	   }  
	 
	   public static void main(String args []) {  
	 
		   new Inventory2();  
	 
	   } //end method main  
	
}
		class DVD
		{
		   public double dvdItemNumber; 
		   public String dvdTitle;
		   public double dvdStock;
		   public double dvdPrice;
		   
		   // four-argument constructor
		   DVD(double item,String title, double stock, double price ) 
		   {  
			   dvdItemNumber  = item;
			   dvdTitle = title;  
			   dvdStock = stock;  
			   dvdPrice = price;  
				 
		   } //end four-argument constructor
		   
			// set dvd item number
		   public void setdvdItemNumber (double item)
		   {
			   dvdPrice= item;
		   }// end method setdvdItemNumber
		   
		   
		   // return dvd item number
		   public double getdvdItemNumber()
		   {
			   return dvdItemNumber;
		   }// end method getdvdItemNumber
		   
		   // set dvd title
		   public void setdvdTitle (String title)
		   {
			   dvdTitle= title;
		   }// end method setdvdTitle
		   
		   // return dvd title
		   public String getdvdTitle()
		   {
			   return dvdTitle;
		   }// end method getdvdTitle
		   
		   // set dvd stock
		   public void setdvdStock(double stock)
		   {
			   dvdStock= stock;
		   } // end method getdvdStock
		   
		   // return dvd stock
		   public double getdvdStock()
		   {
			   return dvdStock;
		   }// end method getdvdStock
		   
		   // set dvd price
		   public void setdvdPrice (double price)
		   {
			   dvdPrice= price;
		   }// end method setdvdPrice
		   
		   
		   // return dvd price
		   public double getdvdPrice()
		   {
			   return dvdPrice;
		   }// end method getdvdPrice
			   
		  
			   
		   // caluclate Inventory
		   public double Calvalue() {  
			   return dvdPrice * dvdStock;  
		   } //end method value  
		   
		   public int compareTo(Object o)
				{
					DVD p = null;
						try
							{
								p = (DVD) o;
							}
						
						catch (ClassCastException cE)
							{
								cE.printStackTrace();
							}
						
						return dvdTitle.compareTo(p.getdvdTitle());
				}

		   
	@Override
		  public String toString()
		  {
			  return String.format( "%d\n,%s\n,%d\n,$%.2f\n,$%.2f",
getdvdTitle(), getdvdItemNumber(), getdvdStock(), getdvdPrice(),Calvalue() ); 
					
		  } // end method toString
			   
	 
		   
	  
   } //end class DVD 



Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main" java.util.IllegalFormatConversion

#2 JeroenFM  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 18
  • View blog
  • Posts: 195
  • Joined: 30-June 08

Re: Exception in thread "main" java.util.IllegalFormatConversion

Posted 21 July 2008 - 03:26 PM

Your String.format call is incorrect. %d stands for Decimal, and must be a number, not a String.
Was This Post Helpful? 0
  • +
  • -

#3 UofPgirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-July 08

Re: Exception in thread "main" java.util.IllegalFormatConversion

Posted 21 July 2008 - 03:54 PM

View PostJeroenFM, on 21 Jul, 2008 - 03:26 PM, said:

Your String.format call is incorrect. %d stands for Decimal, and must be a number, not a String.

So are you saying my format is out of order, or it is completely wrong?
Was This Post Helpful? 0
  • +
  • -

#4 lordms12  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • Posts: 339
  • Joined: 16-February 08

Re: Exception in thread "main" java.util.IllegalFormatConversion

Posted 21 July 2008 - 04:03 PM

"%s\n, %f\n, %f\n, $%.2f\n, $%.2f"
Was This Post Helpful? 0
  • +
  • -

#5 UofPgirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-July 08

Re: Exception in thread "main" java.util.IllegalFormatConversion

Posted 21 July 2008 - 04:46 PM

View Postlordms12, on 21 Jul, 2008 - 04:03 PM, said:

"%s\n, %f\n, %f\n, $%.2f\n, $%.2f"

Thanks, I applicate your help; but if you can look at this and tell me why only one of them is still way off, I made it bold.
init:
deps-jar:
Compiling 1 source file to C:\NetBeansProjects\SCHOOL1\JavaApplication27\build\classes
compile-single:
run-single:
Item number: 685.0
Title: How to Loose a Guy in 10 days
The number of units in stock is 4.0
The price of each DVD is $19.99
The value of the inventory is $79.96
Item number: 565.0
Title: Coyote Ugly Ugly
The number of units in stock is 8.0
The price of each DVD is $19.98
The value of the inventory is $159.84
Item number: 785.0
Title: Legally Blonde
The number of units in stock is 10.0
The price of each DVD is $19.99
The value of the inventory is $199.89999999999998
Item number: 578.0

Title: Sweet Home Alabama
The number of units in stock is 8.0
The price of each DVD is $18.56
The value of the inventory is $148.48
BUILD SUCCESSFUL (total time: 1 second)
Was This Post Helpful? 0
  • +
  • -

#6 lordms12  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • Posts: 339
  • Joined: 16-February 08

Re: Exception in thread "main" java.util.IllegalFormatConversion

Posted 21 July 2008 - 04:51 PM

.2f means that you are only allowed two digits after dot
Was This Post Helpful? 0
  • +
  • -

#7 UofPgirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-July 08

Re: Exception in thread "main" java.util.IllegalFormatConversion

Posted 21 July 2008 - 06:08 PM

Thanks I think I fixed it, Now one last quesion. I am supposed to have a Total Inventory Calculation but I am not sure where to put it how how exactly it is supposed to look, any ideas for me?
Was This Post Helpful? 0
  • +
  • -

#8 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Exception in thread "main" java.util.IllegalFormatConversion

Posted 21 July 2008 - 07:10 PM

Renamed topic title to be more descriptive.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8032
  • View blog
  • Posts: 31,202
  • Joined: 06-March 08

Re: Exception in thread "main" java.util.IllegalFormatConversion

Posted 21 July 2008 - 07:20 PM

		double totalInStock = 0.0;
		double totalValue = 0.0;
		for(int i = 0; i < 4; i++) 
		{  
			   System.out.println(dvd[i]);  
			   System.out.println("The item number is " + dvd[i].getdvdItemNumber());
			   System.out.println("Product Title is " + dvd[i].getdvdTitle());  
			   System.out.println("The number of units in stock is" + dvd[i].getdvdStock());  
			   totalInStock += dvd[i].getdvdStock();
			   System.out.println("The price of each DVD is" + dvd[i].getdvdPrice());
			   System.out.println("The value of the inventory is" + dvd[i].Calvalue());  
			   totalValue += dvd[i].Calvalue();
		  }  
		  System.out.println("Totals: " + totalInStock + " " + totalValue);


Was This Post Helpful? 0
  • +
  • -

#10 UofPgirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-July 08

Re: Exception in thread "main" java.util.IllegalFormatConversion

Posted 21 July 2008 - 08:29 PM

Ok, Here is what it looks like now, after making the changes above: There are many things not correct; just I am not sure how to fix them, I made the mistakes in bold in hopes someone can help me.
init:
deps-jar:
compile-single:
run-single:
[/b]How to Loose a Guy in 10 days
685.000000
4.000000
$19.99
$79.96 $0.00
Item number: 685.0
Title: How to Loose a Guy in 10 days
In Stock: 4.0
Price: $19.99
In Stock Value: $79.96
Coyote Ugly Ugly
565.000000
8.000000
$19.98
$159.84 $0.00

Item number: 565.0
Title: Coyote Ugly Ugly
In Stock: 8.0
Price: $19.98
In Stock Value: $159.84
Legally Blonde
785.000000
13.000000
$19.99
$259.87 $0.00

Item number: 785.0
Title: Legally Blonde
In Stock: 13.0
Price: $19.99
In Stock Value: $259.87
Sweet Home Alabama
578.000000
8.000000
$18.56
$148.48 $0.00

Item number: 578.0
Title: Sweet Home Alabama
In Stock: 8.0
Price: $18.56
In Stock Value: $148.48
Total In Stock: 33.0 Total Value: $648.15
BUILD SUCCESSFUL (total time: 0 seconds)

My current code:
package javaapplication27;





public class Inventory2{
	  
	  
		Inventory2() {	// constructor  
			double totalInStock = 0.0;
			double totalValue = 0.0;  
		   DVD[] dvd =  new DVD[4];  
	  
			dvd[0] = new DVD(685.0,"How to Loose a Guy in 10 days", 4, 19.99 );  
			dvd[1] = new DVD(565.0,"Coyote Ugly Ugly", 8, 19.98 );  
			dvd[2] = new DVD(785.0,"Legally Blonde", 13, 19.99);  
			dvd[3] = new DVD(578.0,"Sweet Home Alabama",8,18.56); 
		   
			
			  for(int i = 0; i < 4; i++) 
			  {  
				 
					   
			   System.out.println(dvd[i]);  
			   System.out.println("Item number: " + dvd[i].getdvdItemNumber());
			   System.out.println(" Title:  " + dvd[i].getdvdTitle());  
			   System.out.println("In Stock: " + dvd[i].getdvdStock());  
			   totalInStock += dvd[i].getdvdStock();
			   System.out.println("Price: $" + dvd[i].getdvdPrice());
			   System.out.println("In Stock Value: $" + dvd[i].Calvalue());  
			   totalValue += dvd[i].Calvalue();
		  }  
			   System.out.println("Total In Stock: " + totalInStock + " Total Value: $" + totalValue); 
		   
	 
	   }  
	 
	   public static void main(String args []) {  
	 
		   new Inventory2();  
	 
	   } //end method main  
	
}
		class DVD
		{
		   public double dvdItemNumber; 
		   public String dvdTitle;
		   public double dvdStock;
		   public double dvdPrice;
		   public double totalInStock;
		   public double totalValue;
		   
		   // four-argument constructor
		   DVD(double item,String title, double stock, double price ) 
		   {  
			   dvdItemNumber  = item;
			   dvdTitle = title;  
			   dvdStock = stock;  
			   dvdPrice = price;  
				 
		   } //end four-argument constructor
		   
			// set dvd item number
		   public void setdvdItemNumber (double item)
		   {
			   dvdPrice= item;
		   }// end method setdvdItemNumber
		   
		   
		   // return dvd item number
		   public double getdvdItemNumber()
		   {
			   return dvdItemNumber;
		   }// end method getdvdItemNumber
		   
		   // set dvd title
		   public void setdvdTitle (String title)
		   {
			   dvdTitle= title;
		   }// end method setdvdTitle
		   
		   // return dvd title
		   public String getdvdTitle()
		   {
			   return dvdTitle;
		   }// end method getdvdTitle
		   
		   // set dvd stock
		   public void setdvdStock(double stock)
		   {
			   dvdStock= stock;
		   } // end method getdvdStock
		   
		   // return dvd stock
		   public double getdvdStock()
		   {
			   return dvdStock;
		   }// end method getdvdStock
		   
		   // set dvd price
		   public void setdvdPrice (double price)
		   {
			   dvdPrice= price;
		   }// end method setdvdPrice
		   
		   
		   // return dvd price
		   public double getdvdPrice()
		   {
			   return dvdPrice;
		   }// end method getdvdPrice
			   
		  
			   
		   // caluclate Inventory
		   public double Calvalue() {  
			   return dvdPrice * dvdStock;  
		   } //end method value  
		   
		  // calculates Total Value
			 public double totalValue(){
					 return dvdPrice * totalInStock;
			 } // end method value
		   
		   public int compareTo(Object o)
				{
					DVD p = null;
						try
							{
								p = (DVD) o;
							}
						
						catch (ClassCastException cE)
							{
								cE.printStackTrace();
							}
						
						return dvdTitle.compareTo(p.getdvdTitle());
				}

		   
	@Override
		  public String toString()
		  {
			  return String.format( "%s\n %f\n %f\n $%.2f\n $%.2f $%.2f",
getdvdTitle(), getdvdItemNumber(), getdvdStock(), getdvdPrice(),Calvalue(),totalValue()); 
					
		  } // end method toString
			   
	 
		   
	  
   } //end class DVD 


Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8032
  • View blog
  • Posts: 31,202
  • Joined: 06-March 08

Re: Exception in thread "main" java.util.IllegalFormatConversion

Posted 21 July 2008 - 09:16 PM

You are printing twice every element
One with the toString() method and once by retreiving all elements one by one

			 for(int i = 0; i < 4; i++) 
			  {					
 //////////	System.out.println(dvd[i]);  
			   System.out.println("Item number: " + dvd[i].getdvdItemNumber());
			   System.out.println(" Title:  " + dvd[i].getdvdTitle());  
			   System.out.println("In Stock: " + dvd[i].getdvdStock());  
			   totalInStock += dvd[i].getdvdStock();
			   System.out.println("Price: $" + dvd[i].getdvdPrice());
			   System.out.println("In Stock Value: $" + dvd[i].Calvalue());  
			   totalValue += dvd[i].Calvalue();
		  }  
			   System.out.println("Total In Stock: " + totalInStock + " Total Value: $" + totalValue); 
 


generates

Item number: 685.0
Title: How to Loose a Guy in 10 days
In Stock: 4.0
Price: $19.99
In Stock Value: $79.96
Item number: 565.0
Title: Coyote Ugly Ugly
In Stock: 8.0
Price: $19.98
In Stock Value: $159.84
Item number: 785.0
Title: Legally Blonde
In Stock: 13.0
Price: $19.99
In Stock Value: $259.87
Item number: 578.0
Title: Sweet Home Alabama
In Stock: 8.0
Price: $18.56
In Stock Value: $148.48
Total In Stock: 33.0 Total Value: $648.15
Was This Post Helpful? 0
  • +
  • -

#12 UofPgirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-July 08

Re: Exception in thread "main" java.util.IllegalFormatConversion

Posted 21 July 2008 - 09:31 PM

Ok, I see this; but if I remove my toString() them I get this:
init:
deps-jar:
compile-single:
run-single:
javaapplication27.DVD@19821f
Item number: 685.0
Title: How to Loose a Guy in 10 days
In Stock: 4.0
Price: $19.99
In Stock Value: $79.96
javaapplication27.DVD@addbf1
Item number: 565.0
Title: Coyote Ugly Ugly
In Stock: 8.0
Price: $19.98
In Stock Value: $159.84
javaapplication27.DVD@42e816
Item number: 785.0
Title: Legally Blonde
In Stock: 13.0
Price: $19.99
In Stock Value: $259.87
javaapplication27.DVD@9304b1
Item number: 578.0
Title: Sweet Home Alabama
In Stock: 8.0
Price: $18.56
In Stock Value: $148.48
Total In Stock:33.0 Total Value: $648.15
BUILD SUCCESSFUL (total time: 0 seconds)
Was This Post Helpful? 0
  • +
  • -

#13 UofPgirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-July 08

Re: Exception in thread "main" java.util.IllegalFormatConversion

Posted 21 July 2008 - 09:50 PM

I got it to work!! Thanks so much!
I promise this will be my last question on this Assignment; if you can tell me if i have another method to sort the array item by mane of product?? I am confused on this, and i can not located it in my book.
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8032
  • View blog
  • Posts: 31,202
  • Joined: 06-March 08

Re: Exception in thread "main" java.util.IllegalFormatConversion

Posted 22 July 2008 - 04:24 AM

Your DVD class must implements Comparable
and provide a compareTo method that returns a number < 0 if smaller, > 0 if bugger, 0 if equals
String already has a comparteTo method so that is easy

class DVD implements Comparable {
....
....
	 DVD() {
	 ....
	 }
	  // returns smaller, bigger or equals according to dvdTitle
	  public int compareTo(Object o) {
		  DVD other = (DVD) o;
		  return dvdTitle.compareTo(other.dvdTitle);
	  }



Now in your Inventory2 class

	  Arrays.sort(dvd];



will sort your array of DVD.
Was This Post Helpful? 0
  • +
  • -

#15 UofPgirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-July 08

Re: Exception in thread "main" java.util.IllegalFormatConversion

Posted 22 July 2008 - 09:15 AM

I am really wondering where I would put the Arrays.sort(dvd]; in that class?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2