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

Thread: New to java, help with printing double

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default New to java, help with printing double

    I have been messing around with java for a small while now,
    i can already code with C.

    Can anyone tell me however why the following wont print

    double fahrenheit = 500;
    double celsius = (fahrenheit - 32) * (5/9.0);
    System.out.println("The value is: %5.2f",celsius);

    But if i printed
    double fahrenheit = 500;
    double celsius = (fahrenheit - 32) * (5/9.0);
    System.out.println("The value is: "+celsius);

    it would just print what is stored in celsius.

    I understand that celsius is a a double so why wont it print when do it the first way

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: New to java, help with printing double

    why the following wont print
    Please copy the full text of the error message and paste it here so we can see the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2017
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to java, help with printing double

    /*
    * This program demonstrates implicit and explicit data conversion
    */
    package dataconversion;

    /**
    *
    * @author Peggy Fisher
    */
    public class DataConversion {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // System.out.println ((3+5+8)/3);
    // System.out.println ((3+5+8)/3.0);
    // double volume = 4/3 * Math.PI * 10 * 10 * 10;
    // double realVolume = 4/3.0 * Math.PI * 10 * 10 * 10;
    //System.out.println("The volume of a sphere with a radius of 10 is: "+volume);
    //System.out.println("The volume of a sphere with a radius of 10 is: "+realVolume);


    double fahrenheit = 500;
    double celsius = (fahrenheit - 32) * (5/9.0);
    System.out.println("The value is: %5.2f",celsius);

    }

    }

    run:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.io.PrintStream.println
    at dataconversion.DataConversion.main(DataConversion. java:26)
    C:\Users\Ollie\AppData\Local\NetBeans\Cache\8.2\ex ecutor-snippets\run.xml:53: Java returned: 1
    BUILD FAILED (total time: 0 seconds)

    --- Update ---

    i have just found out printf works
    what is the difference between printf and println

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: New to java, help with printing double

    what is the difference between printf and println
    Start by reading the API doc for those two methods. It will be explained there.
    http://docs.oracle.com/javase/8/docs/api/index.html

    If you have questions about what is explained there, copy the text that is in question and paste it here with your question.

    A quick and dirty explanation: f is print with formatting, ln is print a newline at end
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help me to round off double, PLEASE JAVA MASTERS!!!
    By iamprogrammer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 8th, 2014, 02:12 AM
  2. Elements in double array (java)
    By wrx12 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 9th, 2012, 11:23 PM
  3. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  4. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM