Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Question Commas not appear in format displayed in JOptionPane

    This program runs as it should. Right now my problem is simply that the commas are appear in the proper places while being displayed in JOptionPane. The assignment says to display the output in JOptionPane and console output, one displays commas properly(console) the other does not.
    Java Code:
    import javax.swing.JOptionPane;
    import java.util.Scanner;
    import java.text.DecimalFormat;
    
    public class Project6
    {
    	public static void main(String[] args)
    	{
    		int numOrdered;
    		double diamondCost;		// 
    		double settingCost;		// 
    		double baseCost;			//sum of the values in the variables for diamond and  setting
    		double totalCost;		
    		double laborCost;		
    		double stateTax;			
    		double luxuryTax;		
    		double finalAmountDue;	
    		final double STATERATE= 0.1;
    		final double LUXURYRATE=0.2;
    		final double LABORRATE=0.05;
    		
    		Scanner keyboard = new Scanner(System.in);
    		
    		System.out.print("What is the Diamind Cost? ");
    		diamondCost = keyboard.nextDouble();
    		System.out.print("\n");
    		
    		System.out.print("What is the Setting Cost? ");
    		settingCost = keyboard.nextDouble();
    		System.out.print("\n");
    		
    		System.out.print("How many of these rings would you like? ");
    		numOrdered = keyboard.nextInt();
    		System.out.print("\n");
    		
    		baseCost = diamondCost + settingCost;
    		luxuryTax = calcExtraCost(baseCost, LUXURYRATE);
    		stateTax = calcExtraCost(baseCost, STATERATE);
    		laborCost = calcExtraCost(baseCost, LABORRATE);
    		totalCost = baseCost+luxuryTax+stateTax+laborCost;
    		finalAmountDue = calcExtraCost(totalCost, numOrdered);
    		
    		System.out.println("Jasmine Jewelry:TOTAL COST BREAKDOWN");
    		System.out.printf("Diamond Cost: ----- $%,.2f\n", diamondCost);
    		System.out.printf("Setting Cost: ----- $%,.2f\n", settingCost);
    		System.out.printf("State Tax @ 10%%: ----- $%,.2f\n", stateTax);
    		System.out.printf("Luxury Tax @ 20%%: ----- $%,.2f\n", luxuryTax);
    		System.out.printf("Labor Cost @ 5%%: ----- $%,.2f\n", laborCost);
    		System.out.printf("Total Price each: ----- $%,.2f\n", totalCost);
    		System.out.println();
    		System.out.println("Number ordered: " + numOrdered);
    		System.out.println();
    		System.out.printf("Total Amount Due: $%,.2f", finalAmountDue);
    		
    		DecimalFormat dollarFormat = new DecimalFormat("#,##0.00");
    
    		JOptionPane.showMessageDialog(null, "Jasmine Jewelry:TOTAL COST BREAKDOWN\n" + "Diamond Cost: ----- $" + diamondCost + "\n" +
    		"Setting Cost: ----- $" + settingCost +  "\n" + "State Tax @ 10%%: ----- $" + stateTax + "\n" + "Luxury Tax @ 20%%: ----- $" +
    		luxuryTax + "\n" + "Labor Cost @ 5%%: ----- $" + laborCost + "\n" + "Total Price each: ----- $" + totalCost + "\n\n" +
    		"Number ordered: " + numOrdered + "\n\nTotal Amount Due: $" + finalAmountDue);
    	
    	public static double calcExtraCost(double num1, double num2)
    	{
    		double result;
    		
    		result = num1 * num2;
    		
    		return result;
    	}
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Commas not appear in format displayed in JOptionPane

    one displays commas properly(console) the other does not
    You declare dollarFormat but never use it anywhere.

    ---

    As an aside, if I wanted the same (or very closely similar) output in two places, I think I would avoid using *both* DecimalFormat and the printf() family if I could.

  3. #3
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Commas not appear in format displayed in JOptionPane

    Thanks, accidentally over-looked that part when searching for the answer in my book.

    and as a side note-
    Yes, that would make sense. Though, my teacher has kinda gone crazy since I challenge him on his grading of one of my assignments. So now he's mixing everything up, I am quite sure he's doing this on purpose.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Commas not appear in format displayed in JOptionPane

    Glad you've got it sorted out. The two formatting methods have different capacities (as revealed by the API docs), about the only purpose I can think of for insisting on both would be to gain some appreciation of those differences.

  5. #5
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Commas not appear in format displayed in JOptionPane

    it should be like this right?
    Java Code:
    		JOptionPane.showMessageDialog(null, "Jasmine Jewelry:TOTAL COST BREAKDOWN\n" + "Diamond Cost: ----- $" + dollarFormat.format(diamondCost) + "\n" +
    "Setting Cost: ----- $" + dollarFormat.format(settingCost) +  "\n" + "State Tax @ 10%%: ----- $" + dollarFormat.format(stateTax) + "\n" +
    "Luxury Tax @ 20%%: ----- $" + dollarFormat.format(luxuryTax) + "\n" + "Labor Cost @ 5%%: ----- $" + dollarFormat.format(laborCost) +
    "\n" + "Total Price each: ----- $" + dollarFormat.format(totalCost) + "\n\n" +"Number ordered: " + dollarFormat.format(numOrdered) +
    "\n\nTotal Amount Due: $" + dollarFormat.format(finalAmountDue));
    but the books never gave an example even remotely like this, it was either formatting a number by itself or showing the printf. it wasn't until i re-check my method section that i found my answer.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Commas not appear in format displayed in JOptionPane

    it should be like this right?
    Well, only you know if the output is correct! :)

    I general long this+that+theOther lines of code are ugly. A shade less ugly is to use indentation. The rules are:

    * Use twice the ordinary indentation when indenting oversized lines
    * Break into bits that go together. So in (foo*baz)+(oof*zab) if you had to break, do so on the +, not one of the *.

    Also you can introduce a new variable, solely for legibility.

    This would give:

    Java Code:
    String outputStr = "Jasmine Jewelry:TOTAL COST BREAKDOWN\n" 
            + "Diamond Cost: ----- $" + dollarFormat.format(diamondCost) + "\n" 
            + "Setting Cost: ----- $" + dollarFormat.format(settingCost) +  "\n" 
            + "State Tax @ 10%%: ----- $" + dollarFormat.format(stateTax) + "\n" 
            + "Luxury Tax @ 20%%: ----- $" + dollarFormat.format(luxuryTax) + "\n" 
            + "Labor Cost @ 5%%: ----- $" + dollarFormat.format(laborCost) + "\n" 
            + "Total Price each: ----- $" + dollarFormat.format(totalCost) + "\n\n" 
            + "Number ordered: " + dollarFormat.format(numOrdered) + "\n\n"
            + "Total Amount Due: $" + dollarFormat.format(finalAmountDue);
    JOptionPane.showMessageDialog(null, outputStr);
    (or put the newlines at the start of the strings - but be consistent)

    Or use a StringBuilder or StringBuffer. These things are made for building strings:

    Java Code:
    StringBuilder buf = new StringBuilder();
    buf.append("Jasmine Jewelry:TOTAL COST BREAKDOWN\n");
    buf.append("Diamond Cost: ----- $").append(dollarFormat.format(diamondCost)).append("\n"); 
    buf.append("Setting Cost: ----- $").append(dollarFormat.format(settingCost)).append("\n");
    // etc 
    JOptionPane.showMessageDialog(null, buf.toString());
    Although you can chain append()s together as shown, overdoing that will end up reproducing the long line problem.

    Note that StringBuilder works with the printf() family:

    Java Code:
    StringBuilder buf = new StringBuilder();
    Formatter fmt = new Formatter(buf);
    buf.append("Jasmine Jewelry: TOTAL COST BREAKDOWN\n");
    fmt.format("Diamond Cost: ----- $%,.2f\n", diamondCost); 
    fmt.format("Setting Cost: ----- $%,.2f\n", settingCost);
    // etc
    JOptionPane.showMessageDialog(null, buf.toString());
    Almost readable, and my preferred option here - but not allowed by your assignment rules.
    Last edited by pbrockway2; 10-29-2012 at 02:21 AM.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Commas not appear in format displayed in JOptionPane

    Another way: JOptionPane will display a String[] array with each element of the array on its own line.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Commas not appear in format displayed in JOptionPane

    True, I should have thought of that.

    And, now thinking about it some more, the fonts used by the dialog may not play well with the intended "columns". I can't stand print or screen output that is ever so slightly wiggily because someone can't control the whitespace.

  9. #9
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Commas not appear in format displayed in JOptionPane

    wow all of these are great suggestions.. i'm definitely referring to this section later when we learn more. thanks guys!

Similar Threads

  1. Getting rid of commas in large numbers?
    By wwuster in forum Advanced Java
    Replies: 12
    Last Post: 03-05-2012, 10:35 AM
  2. Replies: 0
    Last Post: 02-09-2012, 09:25 AM
  3. Saving output displayed in a frame in image format
    By ankit1801 in forum New To Java
    Replies: 1
    Last Post: 05-25-2011, 02:28 PM
  4. Removing All Commas from a String
    By andy9507 in forum New To Java
    Replies: 6
    Last Post: 05-12-2011, 04:16 AM
  5. Replies: 1
    Last Post: 02-20-2009, 02:06 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
  •