Results 1 to 7 of 7
Thread: String output showing "null"
- 11-05-2010, 01:53 AM #1
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
String output showing "null"
Hey guys, beginner here....(Again) here is the latest code i have (2 .java files) and my output it showing "null" for the Part # and part Description when it runs, im new and i don't know what im not doing correctly.
program 2Java Code:// Ex3.13 Invoice.java (JHtP page104) //invoice a hardware store might use to represent an invoice //for an item sold at the store public class Invoice { private String partNumber; // part number for invoice private String partDescription; //part description for invoice private int quantity; //number of parts being purchased private double pricePerItem; // price per item private double invoiceAmount; // total of quantity * priceperitem // 4-arg constructor public Invoice (String partNumber, String partDescription, int count, double price, double invoiceAmount) { setpartNumber ( partNumber ); setpartDescription ( partDescription ); if (count > 0) //determine whether count is positive quantity = count; // valid count assigned to quantity if (price > 0.0) // determine whether price is positive pricePerItem = price; // valid price assign to pricePerItem setquantity ( quantity ); setpricePerItem ( price ); setinvoiceAmount ( invoiceAmount ); } // end 4-arg constructor // set Part number: public void setpartNumber ( String partNumber) { partNumber = partNumber; //accept the passed part value-assgn to instance v partNumber }// end method setpartNumber // get Part number: public String getpartNumber () { return partNumber; } //end method getpartNumber // set partDescription: public void setpartDescription ( String partDescription ) { partDescription = partDescription; // accept the passed part value-assgn to instance v partDesc }// end method setpartDescription //get partDescription: public String getpartDescription () { return partDescription; }// end method getpartDescription // set quantity: public void setquantity ( int count) { quantity = count; }// end method set quantity // get quantity: public int getquantity () { return quantity; } //end method get quantity // set pricePerItem public void setpricePerItem ( double price ) { pricePerItem = price; }// end method set pricePerItem //get pricePerItem public double getpricePerItem () { return pricePerItem; } // end method get pricePerItem //method to setInvoiceAmount public void setinvoiceAmount ( double invoiceAmount ) { invoiceAmount = quantity * pricePerItem; }//end method set invoiceAmount // method to getinvoiceAmount (multiplies quantity * price per item) public double getinvoiceAmount () { return quantity * pricePerItem; }// end method getinvoiceAmount } // end public class Invoice
the output is showing this:Java Code:// Ex3.13 InvoiceTest.java (JHtP page104) // public class InvoiceTest { public static void main ( String args [] ) { //create instance of Invoice passing 4 argument values Invoice invoice1 = new Invoice ( "1234" , "Hammer" , 5 , 14.99, 2 * 14.99 ); //display invoice1 System.out.println ("Original invoice information"); System.out.printf ( "Part Number: %s\n", invoice1.getpartNumber () ); System.out.printf ( "Part Description: %s\n", invoice1.getpartDescription () ); System.out.printf ( "Quantity: %s\n", invoice1.getquantity () ); System.out.printf ( "Price Per Item: %s\n", invoice1.getpricePerItem () ); System.out.printf ( "Total Invoice Amount: %s\n", invoice1.getinvoiceAmount () ); } //end main method }//end public class InvoiceTest
c:\SimplyJava\Invoice>java InvoiceTest
Original invoice information
Part Number: null
Part Description: null
Quantity: 5
Price Per Item: 14.99
Total Invoice Amount: 74.95
c:\SimplyJava\Invoice>
- 11-05-2010, 02:05 AM #2
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
Well firstly don't make a new thread, you're other question (which is the same) is only 4 theads below and posted less than an hour ago!
be patient
Secondly with all the printF statements and methods and params and substitutions you should know about strings. if not you may need to read one of these
http://tinyurl.com/39xzz7uLast edited by maknib; 11-05-2010 at 02:16 AM.
- 11-05-2010, 08:17 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
This is wrong (the same mistake occurs in the other setter methods); your parameter shadows your member variable (they have the same name) so all your method does is set the parameter to its own value; change your method(s) to this:
kind regards,Java Code:public void setpartNumber ( String partNumber) { this.partNumber = partNumber; }
Jos
- 11-05-2010, 09:33 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Oh great.
I could have saved myself a post!
Why ask the question in the other thread and then go and post a new thread??
Bah!
- 11-05-2010, 09:49 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
- 11-05-2010, 10:05 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
You?
Innocent?
Hah!
;)
- 11-05-2010, 10:21 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
string comparison with "=" and ".equal"
By guavajuice in forum New To JavaReplies: 9Last Post: 04-22-2010, 09:01 PM -
Showing/Hiding groups with "triangle flippities"
By Kendall in forum AWT / SwingReplies: 2Last Post: 03-30-2010, 05:07 PM -
Error showing "Object Required"
By kishan in forum Advanced JavaReplies: 2Last Post: 09-26-2009, 02:23 PM -
final String currentWorld = "Java Forums"; String.format("Hello %s", currentWorld);
By mcfrog in forum IntroductionsReplies: 0Last Post: 04-02-2009, 07:02 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks