Inventory Program Part 1

Having major issues with rounding/truncating decimal value for Invento

Page 1 of 1

12 Replies - 2798 Views - Last Post: 30 August 2009 - 09:12 PM Rate Topic: -----

#1 Buttafly101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 09

Inventory Program Part 1

Posted 30 August 2009 - 02:06 PM

To be quite hones, I am not sure as to how to make this work. When I compile and run the code without trying to round or truncate the Inventory Value I receive results like: 432.9876666666666. For display purposes, I would like to know/understand how to show the inventory value to 2 points after the decimal. I have attempted different methods using Half_Up, RoundinMode[], now just importing java.lang.Math.* and attempting to initialize (with errors). Here is what I have...can someone please help me learn and get this to work?

// Inventory Program Part 1  
/* Choose a product that lends itself to an inventory (for example; products at your workplace, 
   office supplies, music CDs, DVD movies, or software). */  
/* Create a Java application that displays:
   product number  prodNum
	name of the product  prodName
   number of units in stock  unitStock
	price of each unit  unitPrice
	value of the inventory (number of units in stock * price of each unit).  invValue */
import static java.lang.Math.*;

public class Inventory1
{
	// main method begins execution of Java application
	public static void main( String args[] )
	{

		DecimalFormat df = new DecimalFormat( double df( ###0.00 ) );
		
		Software software;
		
		software = new Software( "Adobe Dreamweaver CS3", 1532.01, 8, 423.98 );
			System.out.println( software );
			System.out.println( "\nSoftware number: dev" + software.getProdNum() );
			System.out.println( "Software title: " + software.getProdName() );
			System.out.println( "Inventory on hand: " + software.getUnitStock() );
			System.out.println( "Software price each: " + software.getUnitPrice() );
			System.out.printf( "Inventory value: $%.2f\n" + software.invValue() );

		software = new Software( "Adobe PhotoShop Elements CS3", 1450.03, 11, 450.98 );
			System.out.println( software );
			System.out.println( "\nSoftware number: pho" + software.getProdNum() );
			System.out.println( "Software title: " + software.getProdName() );
			System.out.println( "Inventory on hand: " + software.getUnitStock() );
			System.out.println( "Software price each: $" + software.getUnitPrice() );
			System.out.printf( "Inventory value: $%.2f\n" + software.invValue() );
			
		software = new Software( "PDF Converter Professional 5", 1248.02, 7, 99.99 );
			System.out.println( software );
			System.out.println( "\nSoftware number: util" + software.getProdNum() );
			System.out.println( "Software title: " + software.getProdName() );
			System.out.println( "Inventory on hand: " + software.getUnitStock() );
			System.out.println( "Software price each: $" + software.getUnitPrice() );
			System.out.printf( "Inventory value: $%.2f\n" + software.invValue() );
			
		software = new Software( "Quest Toad", 1537.04, 10, 580.98 );
			System.out.println( software );
			System.out.println( "\nSoftware number: dev" + software.getProdNum() );
			System.out.println( "Software title: " + software.getProdName() );
			System.out.println( "Inventory on hand: " + software.getUnitStock() );
			System.out.println( "Software price each: $" + software.getUnitPrice() );
			System.out.printf( "Inventory value: $%.2f\n" + software.invValue() );
			
		software = new Software( "Corel Paint Shop Pro Photo", 1476.02, 6, 69.99 );
			System.out.println( software );
			System.out.println( "\nSoftware number: pho" + software.getProdNum() );
			System.out.println( "Software title: " + software.getProdName() );
			System.out.println( "Inventory on hand: " + software.getUnitStock() );
			System.out.println( "Software price each: $" + software.getUnitPrice() );
			System.out.printf( "Inventory value: $%.2f\n" + software.invValue() );
			
		software = new Software( "System Mechanic", 1283.01, 7, 49.99 );
			System.out.println( software );
			System.out.println( "\nSoftware number: util" + software.getProdNum() );
			System.out.println( "Software title: " + software.getProdName() );
			System.out.println( "Inventory on hand: " + software.getUnitStock() );
			System.out.println( "Software price each: $" + software.getUnitPrice() );
			System.out.printf( "Inventory value: $%.2f\n" + software.invValue() );
		
	} // end method main
	
} // end class Inventory1




Here are the errors I receive:
Inventory1.java:18: '.class' expected
		DecimalFormat df = new DecimalFormat( double df( ###0.00 ) );
													 ^
Inventory1.java:18: ';' expected
		DecimalFormat df = new DecimalFormat( double df( ###0.00 ) );
													   ^
Inventory1.java:18: illegal character: \35
		DecimalFormat df = new DecimalFormat( double df( ###0.00 ) );
														 ^
Inventory1.java:18: illegal character: \35
		DecimalFormat df = new DecimalFormat( double df( ###0.00 ) );
														  ^
Inventory1.java:18: illegal character: \35
		DecimalFormat df = new DecimalFormat( double df( ###0.00 ) );
														   ^
Inventory1.java:18: ';' expected
		DecimalFormat df = new DecimalFormat( double df( ###0.00 ) );
															^
Inventory1.java:18: illegal start of expression
		DecimalFormat df = new DecimalFormat( double df( ###0.00 ) );
																 ^
Inventory1.java:18: ';' expected
		DecimalFormat df = new DecimalFormat( double df( ###0.00 ) );
																  ^
8 errors



Is This A Good Question/Topic? 0
  • +

Replies To: Inventory Program Part 1

#2 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Inventory Program Part 1

Posted 30 August 2009 - 04:07 PM

new DecimalFormat( double df( ###0.00 ) );
Thats the source of what looks like all of your errors, it is a syntax mess.

Why not just take the whole thing out? I don't see anywhere where you actually use it.

Are your printf statements not working? %.2f should cut off all but the first two decimals.

This post has been edited by syfran: 30 August 2009 - 04:09 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Inventory Program Part 1

Posted 30 August 2009 - 04:25 PM

DecimalFormat df = new DecimalFormat( double df( ###0.00 ) );

The DecimalFormat constructor expects a String in its constructor
You probably want

DecimalFormat df = new DecimalFormat("###0.00");
Was This Post Helpful? 1
  • +
  • -

#4 Buttafly101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 09

Re: Inventory Program Part 1

Posted 30 August 2009 - 04:29 PM

View Postsyfran, on 30 Aug, 2009 - 03:07 PM, said:

new DecimalFormat( double df( ###0.00 ) );
Thats the source of what looks like all of your errors, it is a syntax mess.

Why not just take the whole thing out? I don't see anywhere where you actually use it.

Are your printf statements not working? %.2f should cut off all but the first two decimals.



I removed all of the DecimalFormat line...the program will compile. Unfortunately, I get a runtime error (listed below) and something funky with the first output - Software@3e25a5???
Software@3e25a5

Software number: dev1532.01
Software title: Adobe Dreamweaver CS3
Inventory on hand: 8
Software price each: 423.98
Inventory value: $Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '.2f'
	at java.util.Formatter.format(Formatter.java:2432)
	at java.io.PrintStream.format(PrintStream.java:920)
	at java.io.PrintStream.printf(PrintStream.java:821)
	at Inventory1.main(Inventory1.java:24)


From the error...I am guessing that java is not liking printf( "Inventory value: $%.2f\n" ), mainly the .2f part. I am not sure how to fix this, as printf using .2f was an attempt at limiting the decimal points.
Was This Post Helpful? 0
  • +
  • -

#5 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Inventory Program Part 1

Posted 30 August 2009 - 04:33 PM

Every time you put a formater( in this case %.2f) in a printf statement you need an argument that matches it. So instead of
 System.out.printf( "Inventory value: $%.2f\n" + software.invValue() );


you want
 System.out.printf( "Inventory value: $%.2f\n" , software.invValue() );



This tells java that you want to format the second argument (inValue()) to have two decimal places.

If you were to add a second format statement it would be telling java to format the third argument.

This post has been edited by syfran: 30 August 2009 - 04:35 PM

Was This Post Helpful? 1
  • +
  • -

#6 Buttafly101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 09

Re: Inventory Program Part 1

Posted 30 August 2009 - 04:54 PM

View Postsyfran, on 30 Aug, 2009 - 03:33 PM, said:

Every time you put a formater( in this case %.2f) in a printf statement you need an argument that matches it. So instead of
 System.out.printf( "Inventory value: $%.2f\n" + software.invValue() );


you want
 System.out.printf( "Inventory value: $%.2f\n" , software.invValue() );



This tells java that you want to format the second argument (inValue()) to have two decimal places.

If you were to add a second format statement it would be telling java to format the third argument.

You're Awesome! Thank You!!!!! I did as you stated, now th program compiles and runs fine.
Can I ask you another question????
What is this: Software@3e25a5? It is at the start of each of the items groupings. I am not sure why I am getting this strange output, that is not throwing an error.
Software@3e25a5
Software number: dev1532.01
Software title: Adobe Dreamweaver CS3
Inventory on hand: 8
Software price each: 429.98
Inventory value: $3439.84

Software@61de33
Software number: pho1450.03
Software title: Adobe PhotoShop Elements CS3
Inventory on hand: 11
Software price each: $450.98
Inventory value: $4960.78

Software@14318bb
Software number: util1248.02
Software title: PDF Converter Professional 5
Inventory on hand: 7
Software price each: $99.99
Inventory value: $699.93

Software@ca0b6
Software number: dev1537.04
Software title: Quest Toad
Inventory on hand: 10
Software price each: $580.98
Inventory value: $5809.80

Software@10b30a7
Software number: pho1476.02
Software title: Corel Paint Shop Pro Photo
Inventory on hand: 6
Software price each: $69.99
Inventory value: $419.94

Software@1a758cb
Software number: util1283.01
Software title: System Mechanic
Inventory on hand: 7
Software price each: $49.99
Inventory value: $349.93


I think it has something to do with how I have the array setup for each product. Or something I have done in my Product class.
Can you please point me in the right direction?
Was This Post Helpful? 0
  • +
  • -

#7 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Inventory Program Part 1

Posted 30 August 2009 - 05:03 PM

System.out.println( software );



That line is printing it. Without a toString() method to use, java tries its best to give a string representation.

In this case it is just saying that this instance of Software is located at memory address 3e25a5
Was This Post Helpful? 1
  • +
  • -

#8 Buttafly101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 09

Re: Inventory Program Part 1

Posted 30 August 2009 - 05:19 PM

View Postsyfran, on 30 Aug, 2009 - 04:03 PM, said:

System.out.println( software );



That line is printing it. Without a toString() method to use, java tries its best to give a string representation.

In this case it is just saying that this instance of Software is located at memory address 3e25a5


You're the BOMB!!! I really appreciate you taking the time to assist me and explain why I was receiving the errors and forementioned specified output. It makes sense...since I didn't specify where to look/go...I just hadn't seen that type of output before and it had me baffled.
I removed the System.out.println( software ); from the code, as it is actually not needed because I now get properly formatted output for each of my instructors requirements: Product Name, Product Number, Units in Stock, Unit Price, and Stock Value. All is Good!
Was This Post Helpful? 0
  • +
  • -

#9 LynnL  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 109
  • Joined: 13-April 09

Re: Inventory Program Part 1

Posted 30 August 2009 - 06:16 PM

Make up your mind:
- use decimalFormat as PBL suggested to be 100% Java
- use printf as Syfran sugested which is an add on to please C programmer since 1.5 (that will work and which I prefer... sorry PBL)
Was This Post Helpful? 0
  • +
  • -

#10 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Inventory Program Part 1

Posted 30 August 2009 - 06:25 PM

View PostLynnL, on 30 Aug, 2009 - 05:16 PM, said:

Make up your mind:
- use decimalFormat as PBL suggested to be 100% Java
- use printf as Syfran sugested which is an add on to please C programmer since 1.5 (that will work and which I prefer... sorry PBL)


Your a little late to the party... He just removed the whole DecimalFormater thing.

How is printf not 100% java? I just see it as a shortcut to format the string/number. A lot of java syntax is based on C/C++ it is still java.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Inventory Program Part 1

Posted 30 August 2009 - 06:35 PM

View Postsyfran, on 30 Aug, 2009 - 05:25 PM, said:

View PostLynnL, on 30 Aug, 2009 - 05:16 PM, said:

Make up your mind:
- use decimalFormat as PBL suggested to be 100% Java
- use printf as Syfran sugested which is an add on to please C programmer since 1.5 (that will work and which I prefer... sorry PBL)


Your a little late to the party... He just removed the whole DecimalFormater thing.

How is printf not 100% java? I just see it as a shortcut to format the string/number. A lot of java syntax is based on C/C++ it is still java.


You mean my post #3 is completly useless ? :)
I think that what she means (assuming LynnL is a she) is that methods with variable number of arguments (like printf) were not supported before JRE 1.5
and it is with relunctancy, after lot of pressure, that Jave Engineering decided to introduce it in 1.5
Yes it is very usefull
but it can generate run time exeption that Java used to detect at compile time
Was This Post Helpful? 0
  • +
  • -

#12 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Inventory Program Part 1

Posted 30 August 2009 - 07:43 PM

Quote

You mean my post #3 is completly useless ? :)


Of course not but to the OP I'm sure it was a lot easier to remove the one line instead of changing all those printfs. I'm sure he learned from your post though >.>

This post has been edited by syfran: 30 August 2009 - 07:44 PM

Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Inventory Program Part 1

Posted 30 August 2009 - 09:12 PM

View Postsyfran, on 30 Aug, 2009 - 06:43 PM, said:

Quote

You mean my post #3 is completly useless ? :)


Of course not but to the OP I'm sure it was a lot easier to remove the one line instead of changing all those printfs. I'm sure he learned from your post though >.>

I used printf() a lot in debugging code (for a quick one) or for error handling but I use the good old one DecimalFormat() for released code
but this is really personal choice
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1