Results 1 to 7 of 7
  1. #1
    teohzj123 is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default How do I enter decimals in Java

    Hi,I'm new to Java programming and I just started like a day ago.I'm using NetBeans 7.3.1

    My task is to convert any value in degrees celcius to fahrenheit.The task says something about float..I don't know what's float so I didn't use it

    This is what I wrote.Apparently it worked but once I entered decimal values it comes out with an error.Can someone please teach me?And what is float? :)

    package lab1task2;
    import java.util.Scanner;

    public class Lab1Task2 {


    public static void main(String[] args) {

    Scanner input = new Scanner( System.in );
    int number1;
    int sum;

    System.out.print ( "Enter temperature in degrees Celcius=");
    number1 = input.nextInt();

    sum = number1*9/5 +32;

    System.out.printf ( "Temperature in degrees Fahrenheit = %d\n", sum );

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default Re: How do I enter decimals in Java

    Your program uses number without a fractional part (ints); Java has a couple of types that can handle those fractional parts: float, double and BigDecimals; floats and doubles are both primitive types (a double can store larger numbers and more fractional digits than a float); instead of using ints, use floats or doubles; also use the nextDouble() or nextFloat() method of the Scanner object and see the result ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    teohzj123 is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: How do I enter decimals in Java

    How do I do them?I am very new to Java.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default Re: How do I enter decimals in Java

    Quote Originally Posted by teohzj123 View Post
    How do I do them?I am very new to Java.
    As I suggested in my previous reply: int --> double and nextInt() --> nextDouble() or int --> float and nextInt() --> nextFloat().

    kind regards,

    Jos

    ps. and please read the tutorials.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    teohzj123 is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: How do I enter decimals in Java

    Hey thanks..Actually I already did what you asked too but it still didn't work.Then I changed this %d\n" to %.3f\n"..And it worked now..Any ideas why?

    PS:I'm starting to read tutorials now :)
    Last edited by teohzj123; 08-25-2013 at 03:10 PM.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default Re: How do I enter decimals in Java

    Quote Originally Posted by teohzj123 View Post
    Hey thanks..Actually I already did what you asked too but it still didn't work.Then I changed this %d\n" to %.3f\n"..And it worked now..Any ideas why?

    PS:I'm starting to read tutorials now :)
    Ah,yes, I didn't notice the format String; it needed to be changed as well (as you did). "%d" formats an int number while "%3f" formats a float (or double) number. You'll find all the details somewhere in the tutorials (and the API documentation); happy reading, you'll learn a lot from them.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

Similar Threads

  1. How to subtract numbers with decimals with Java?
    By coding in forum New To Java
    Replies: 3
    Last Post: 01-27-2013, 01:47 AM
  2. Java does not read the decimals?
    By Josep_16 in forum New To Java
    Replies: 4
    Last Post: 08-08-2011, 09:23 AM
  3. Replies: 8
    Last Post: 06-06-2011, 02:27 AM
  4. Decimals in java help!!
    By Gold in forum New To Java
    Replies: 3
    Last Post: 12-09-2009, 11:25 PM
  5. Replies: 1
    Last Post: 07-02-2008, 01:58 PM

Tags for this Thread

Posting Permissions

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