Results 1 to 9 of 9
  1. #1
    MooNinja is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Exclamation 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!

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    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)

  3. #3
    MooNinja is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    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

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    it works for me...
    Java Code:
    public class Test {
      public static void main(String[] args){
        String s = String.format("\nPrice: $%.02f \n", 95F);
        System.out.println(s);
      }
    }
    prints $95.00
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    %.2f = 2 max
    %2.2f = 2 min, 2 max

  6. #6
    MooNinja is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    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());

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    My toString won't even compile now
    Always remember to post the compiler's error messages in this situation. It will save us much time and guessing.

  8. #8
    MooNinja is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    I apologize. This is my first time seeking help outside of my manual :)

    That code was my latest.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    My post was not placed to elicit an apology but rather the compiler's error message. Well?

Similar Threads

  1. toString() method
    By 01allenh in forum New To Java
    Replies: 2
    Last Post: 03-25-2009, 11:43 PM
  2. toString question
    By mayhewj7 in forum New To Java
    Replies: 1
    Last Post: 01-29-2009, 07:41 PM
  3. toString method
    By apfroggy0408 in forum New To Java
    Replies: 6
    Last Post: 01-31-2008, 04:08 AM
  4. Arrays.toString
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 09:35 PM
  5. Can i just use toString?
    By cachi in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •