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 6 of 6

Thread: One Decimal Place

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default One Decimal Place

    Hello there... I want to convert the output to one decimal place but what I got is "Cannot format given Object as a Number". Can anyone help me out? Thank you

    //Assignment 1 Question 2
     
    import java.text.*;
     
     
    public class darjahCelcius
     
    {
     
    	public static void main(String[] args) 
     
    		{
     
     
    			System.out.println("Celsius\t\tFahrenheit\t|\tFahrenheit\t\tCelsius");
     
    			System.out.println("----------------------------------------------------------");
     
    			double celsius = 40; double fahrenheit = 120;
     
    			NumberFormat formatter = new DecimalFormat("#0.0");
     
    			for (int i = 1; i <= 10; celsius--, fahrenheit -= 10, i++) 
     
    				{
     
    					System.out.println(formatter.format(celsius + "\t\t" +celsiusToFahrenheit(celsius) + "\t\t|\t" + fahrenheit + "\t\t\t" +
    					fahrenheitToCelsius(fahrenheit)));
     
    				}
     
    		}
     
    			public static double celsiusToFahrenheit(double celsius) 
     
    				{
    					return (9.0 / 5.0) * celsius + 32;
    				}
     
    			public static double fahrenheitToCelsius(double fahrenheit) 
     
    				{
    					return (5.0 / 9) * (fahrenheit - 32);
    			 	}
     
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: One Decimal Place

    The error message is telling you exactly what is wrong. You use a DecimalFormatter to format a number period, but you're trying to pass it a whole complex String complete with several numbers and tabs(?). Instead why not simply pass it the number that you want to format and use its returned String in your output? You can use the formatter as many times as you want if you want to format several numbers, but again, only pass into it a single number.

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    Akirien (August 21st, 2012)

  4. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: One Decimal Place

    Thanks for your reply.. But can you teach me how to do that?

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: One Decimal Place

    Quote Originally Posted by Akirien View Post
    Thanks for your reply.. But can you teach me how to do that?
    I'm trying. Again, what you're doing is creating a DecimalFormat object called formatter, and you're trying to format a non-numeric String. What you've got is:

    String myResultString = formatter.format( this huge complex String with numbers and tabs and what not).

    What you need to do instead is

    String myResultString = "Some complex String " + formatter.format(/*some number goes here*/) + 
              " more String here " + formatter.format(/*another number goes here*/); // etc....

    Clear as Mudd?

  6. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: One Decimal Place

    thanks!!! got it...hehe.. thanx a lot..

  7. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: One Decimal Place

    You're welcome!

Similar Threads

  1. How to think about LOOPS in the first place
    By searchformeaning in forum Object Oriented Programming
    Replies: 6
    Last Post: May 1st, 2012, 01:05 PM
  2. How do I change my output to 1 decimal place?
    By dunnage888 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: November 3rd, 2011, 09:39 AM
  3. A place to call home
    By Tsarin in forum Member Introductions
    Replies: 1
    Last Post: October 14th, 2011, 07:56 AM
  4. I cannot get the right output in the right place. Where is my problem?
    By kl2eativ in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2011, 07:28 AM
  5. Where to place my own library?
    By hexwind in forum Java Theory & Questions
    Replies: 3
    Last Post: June 22nd, 2011, 06:25 AM