Results 1 to 9 of 9
Thread: Formatting a toString
- 03-31-2009, 12:05 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 4
- Rep Power
- 0
Formatting a toString
Ok, in my intro java class we are tasked with creating a program that essentially gathers information from a number of other classes and then prints them out in one of a couple toStrings. This toString has a double (price) and there there is a call to another classes toString Pub1
I can't get this to work. The formatting is off. How would I format this correctly to display price in normal monetary format along with the others ?
return String.format("\n Title: %s \n Author: %s \n ISBN#: %s \n Price: $%.02f \n %s ", title, author, isbn, price, pub1.toString());
Thanks in advance guys!!
I can post more if needed!
- 03-31-2009, 06:40 PM #2
are you talking about price or pub1? what's the output you're getting and and output you want to look like?
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 03-31-2009, 06:42 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 4
- Rep Power
- 0
Well price isn't displaying properly when printed, and I suspect the pub1 is holding up the project from working since it keeps getting the error.
price is displaying
price: $95.0 won't display both decimals
I'd prefer it to show $95.00
- 03-31-2009, 06:54 PM #4
it works for me...
prints $95.00Java Code:public class Test { public static void main(String[] args){ String s = String.format("\nPrice: $%.02f \n", 95F); System.out.println(s); } }USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 03-31-2009, 06:56 PM #5
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
%.2f = 2 max
%2.2f = 2 min, 2 max
- 03-31-2009, 06:59 PM #6
Member
- Join Date
- Mar 2009
- Posts
- 4
- Rep Power
- 0
here is what I am working with. My toString won't even compile now :( I know its a lot so I understand if you don't want to check through it
Java Code:public class booktest { public static void main(String[] args) { book bookArray[] = new book[6]; String dataArray[][] = { { "Book", "Java", "Davis", "123", "Wiley", "London", "25.50" }, { "Book", "C++", "Jones", "456", "Random", "NY", "10.75" }, { "Fiction", "Where's My Car", "Ashton Kutcher", "777", "Oreilly", "Dallas", "5.95" }, { "Fiction", "Doom", "The Rock", "918", "Sans Publishing", "LA", "12.50" }, { "Non-Fiction", "Earthquakes", "Chuck Berry", "435", "LA Books", "LA", "75.00" }, { "Non-Fiction", "Universal Studios", "Walt", "987", "Dell", "Houston", "29.90" } }; int count = 0; for (int i = 0; i < dataArray.length; i++) { count++; if (dataArray[i][0].equals("Book")) bookArray[i] = new book(dataArray[i][1], dataArray[i][2], dataArray[i][3], Double.parseDouble(dataArray[i][6]), new publisher(dataArray[i][4], dataArray[i][5])); else if (dataArray[i][0].equals("Fiction")) bookArray[i] = new fiction(count, dataArray[i][1], dataArray[i][2], dataArray[i][3], Double .parseDouble(dataArray[i][6]), new publisher( dataArray[i][4], dataArray[i][5])); else if (dataArray[i][0].equals("Non-Fiction")) bookArray[i] = new nonfiction(" History ", dataArray[i][1], dataArray[i][2], dataArray[i][3], Double .parseDouble(dataArray[i][6]), new publisher( dataArray[i][4], dataArray[i][5])); } for (int i = 0; i < bookArray.length; i++) System.out.println(bookArray[i].toString()); int totalSold = ((count + 2) * 3); double amountDue = 0; double totalCharges = 0; for (int i = 0; i < bookArray.length; i++){ amountDue = bookArray[i].calculateCharge(totalSold, Double .parseDouble(dataArray[i][6])); totalCharges = totalCharges + amountDue; } System.out.printf("\n The total charges due are $%.02f", totalCharges); }Java Code:public class book { private String title; private String author; private String isbn; private double price; private publisher pub1; public book(String title, String author, String isbn,double price, publisher pub1) { setTitle(title); setAuthor(author); setIsbn(isbn); setPrice(price); setPub1(pub1); } public void setTitle(String title) { this.title = title; } public String getTitle() { return title; } public void setAuthor(String author) { this.author = author; } public String getAuthor() { return author; } public void setIsbn(String isbn) { this.isbn = isbn; } public String getIsbn() { return isbn; } public void setPrice(double price) { this.price = price; } public double getPrice() { return price; } public void setPub1(publisher pub1) { this.pub1 = pub1; } public publisher getPub1() { return pub1; } public double calculateCharge(int totalSold, double price)// ‘calculateCharge’ that accepts the number of books sold and returns the total price { return (price * totalSold); } public String toString() { return("\n Title: " + title + "\n Author: " + author + "\n ISBN#: " + isbn +"\n Price $ "+ price + pub1.toString()); } } //return String.format("\n Title: %s \n Author: %s \n ISBN#: %s \n Price: $%.02f \n %s ", title, author, isbn, price, pub1.toString());
-
Always remember to post the compiler's error messages in this situation. It will save us much time and guessing.My toString won't even compile now
- 03-31-2009, 07:24 PM #8
Member
- Join Date
- Mar 2009
- Posts
- 4
- Rep Power
- 0
I apologize. This is my first time seeking help outside of my manual :)
That code was my latest.
-
Similar Threads
-
toString() method
By 01allenh in forum New To JavaReplies: 2Last Post: 03-25-2009, 11:43 PM -
toString question
By mayhewj7 in forum New To JavaReplies: 1Last Post: 01-29-2009, 07:41 PM -
toString method
By apfroggy0408 in forum New To JavaReplies: 6Last Post: 01-31-2008, 04:08 AM -
Arrays.toString
By Java Tip in forum Java TipReplies: 0Last Post: 12-01-2007, 09:35 PM -
Can i just use toString?
By cachi in forum New To JavaReplies: 1Last Post: 07-31-2007, 08:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks