0

I'm writing a simple program that converts pounds to kilos, but I'm a bit confused

import java.util.Scanner;
public class FiveTenOne {
 public static void main(String[] args)
 {
 Scanner keyboard = new Scanner(System.in); //Creates Scanner Object
 System.out.print("Enter your weight in pounds: ");
 float pounds = keyboard.nextFloat();
 float kilos = pounds / 2.2;
 System.out.print("Your value in kilos is: " + kilos);
 keyboard.close();
 }
}

This is the code I wrote, all variables have been set to floats, yet it tells me that it cant convert from a double. at "pounds / 2.2;"

However when I write it like the code below "(float) (pounds / 2.2)" then no error comes back

import java.util.Scanner;
public class FiveTenOne {
 public static void main(String[] args)
 {
 Scanner keyboard = new Scanner(System.in); //Creates Scanner Object
 System.out.print("Enter your weight in pounds: ");
 float pounds = keyboard.nextFloat();
 float kilos = (float) (pounds / 2.2);
 System.out.print("Your value in kilos is: " + kilos);
 keyboard.close();
 }
}

Can someone explain to me why the first code doesn't take it as a float?

or is it an IDE problem?

I tried both ways and know that the second one works fine, but I want to understand why exactly the first one doesn't work

2
  • 2
    Write 2.2 as 2.2f, so it is a float. No casting needed. Commented Dec 8, 2023 at 11:24
  • 2.2 is a double (not a float) and if one of the operands is double, the result will be double - explained in the Java Language Specification 5.6. Numeric Contexts. See also 3.10.2. Floating-Point Literals. Commented Dec 8, 2023 at 14:06

3 Answers 3

2

float is, at this point, best thought of as a historic abberation. Do not use them; there is no need. double is like float but more precise, and just as fast if not faster. Also, java is designed to prefer them, so simple constants such as 2.2 are actually considered a double. Hence, aFloatValue / 2.2 is a float divided by a double, which java solves by first silently upgrading the float to a double, then doing double division. This produces a double value. Which you try to assign to a variable of type float which is risky (can remove detail - a so called 'narrowing conversion'), which requires an explicit cast so you, the programmer, can tell the compiler: Yes. yes, I heard you, this is dangerous, I know what I'm doing shut up and compile my code please.

The correct answer here is to get rid of all mention of the word float and use double everywhere. You lose nothing, and your code is simpler; no casts would be needed then.

answered Dec 8, 2023 at 13:44
Sign up to request clarification or add additional context in comments.

Comments

1

Double and Float are the datatypes in java, so to convert from one type to the other you have to cast it . For example , pounds/2.2 returns a double value ( one class ) say 20.0. To convert it we have to cast into Float which is 20f.

answered Dec 8, 2023 at 12:34

1 Comment

"pounds/2.2 returns a double value" This would be a much better answer if you explained why that is the case.
0

double and float are different data types, see e.g. Java: double vs float. The error message says that the division pounds / 2.2 returns a double value. If you want the result to be float, the value has to be cast to float explicitly, just like you did in your second code example.

answered Dec 8, 2023 at 10:34

1 Comment

please do not confuse double - a primitive - with Double - a wrapper class!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.