Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: DecimalFormat error when compiled

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default DecimalFormat error when compiled

    I am new to Java, and having problems with the use of DecimalFormat. When I compile the program it gives me an error so it doesn't work. I am not sure what I am doing wrong. I think that I am using the syntax correctly. When I take out the DecimalFormat it compiles and runs, but I would like the output with commas. What am I doing wrong here?

    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    public class yeargas
    {
    	public static void main(String[] args)
    	{
    		int tank, month;
    		double year, yearx ,total;
    		String input;
    		DecimalFormat formatter=new DecimalFormat(0,000);
    		//get the average cost to fill 1 tank of gas
    		input=JOptionPane.showInputDialog("What is the average cost to fill 1 tank of gas? ");
    		tank=Integer.parseInt(input);
    		//get the x number of times you fill in a month
    		input=JOptionPane.showInputDialog("What is the average number of times you fill your car in a month? ");
    		month=Integer.parseInt(input);
    		//calculate the cost for a month
    		//multiply that by 1 year
    		year=(tank*month)*12;
    		//get the x number of years to calculate
    		input=JOptionPane.showInputDialog("How many years do you want to calculate for? ");
    		yearx=Integer.parseInt(input);
    		total=yearx*year;
    		//display the aproximate total cost for x
    		JOptionPane.showMessageDialog(null,"Your average cost of gas for "+yearx+" is $"+(formatter.format(total))+".");//total+".");
    		System.exit(0);
    	}
    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: DecimalFormat error when compiled

    You need to pass DecimalFormat a String. Put quotes around 0,000.
    Also, please post your error in the future.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: DecimalFormat error when compiled

    Thanks for help. I didn't catch it. I am in a room that is about 93 degrees with a fan as my only saving grace.

    How do I do that, so that I can do it the next time around.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: DecimalFormat error when compiled

    Just tell us what error you are getting when you compile it.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: DecimalFormat error when compiled

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/61295-decimal-format-error.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  6. #6
    Junior Member
    Join Date
    Jun 2012
    Posts
    29
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: DecimalFormat error when compiled

    THIS SHOULD RUN NOW
    ...edited by moderator
    Last edited by copeg; July 11th, 2012 at 10:46 PM.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: DecimalFormat error when compiled

    @kindk12, please read http://www.javaprogrammingforums.com...n-feeding.html

Similar Threads

  1. Please! Help me to this error "ERROR CANNOT FIND SYMBOL"
    By mharck in forum Object Oriented Programming
    Replies: 8
    Last Post: July 3rd, 2012, 09:20 AM
  2. no error when compiled but when the program runs .. help this !!!
    By izzahmed in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 8th, 2011, 08:55 AM
  3. [SOLVED] Compiled .jar won't work (Error-sound)
    By Fermen in forum Object Oriented Programming
    Replies: 2
    Last Post: March 31st, 2011, 05:24 PM
  4. MessageDialog working with DecimalFormat?
    By Plural in forum Java Theory & Questions
    Replies: 2
    Last Post: October 12th, 2010, 04:07 AM
  5. Program compiled/ran as required - FAILED project WHY??????????
    By MISSAJ in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 23rd, 2010, 03:35 PM