Results 1 to 7 of 7
Thread: How do I enter decimals in Java
- 08-25-2013, 03:28 PM #1
Member
- Join Date
- Aug 2013
- Posts
- 3
- Rep Power
- 0
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 );
- 08-25-2013, 03:40 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-25-2013, 03:54 PM #3
Member
- Join Date
- Aug 2013
- Posts
- 3
- Rep Power
- 0
Re: How do I enter decimals in Java
How do I do them?I am very new to Java.
- 08-25-2013, 03:58 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: How do I enter decimals in Java
Build a wall around Donald Trump; I'll pay for it.
- 08-25-2013, 04:08 PM #5
Member
- Join Date
- Aug 2013
- Posts
- 3
- Rep Power
- 0
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 04:10 PM.
- 08-25-2013, 04:47 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: How do I enter decimals in Java
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-25-2013, 04:49 PM #7
Re: How do I enter decimals in Java
Cz "d" is for decimal integer and "f" is for float ... for more information visit : Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
Similar Threads
-
How to subtract numbers with decimals with Java?
By coding in forum New To JavaReplies: 3Last Post: 01-27-2013, 02:47 AM -
Java does not read the decimals?
By Josep_16 in forum New To JavaReplies: 4Last Post: 08-08-2011, 10:23 AM -
Beginner trying to write Java code, has issue w/ printing result and 2 decimals
By flpanthers1 in forum New To JavaReplies: 8Last Post: 06-06-2011, 03:27 AM -
Decimals in java help!!
By Gold in forum New To JavaReplies: 3Last Post: 12-10-2009, 12:25 AM -
Java Program To Convert A Number In To Words With Decimals
By javanewbie in forum New To JavaReplies: 1Last Post: 07-02-2008, 02:58 PM
Bookmarks