I have this problem wherein I have to convert kilometers into miles. I'm a novice programmer so bear with me.
Here's my code so far:
import java.util.Scanner;
public class problem1 {
public static void main (String args[]) {
float m;
float km;
Scanner input=new Scanner(System.in);
System.out.print("Please enter a distance in kilometers:");
km=input.nextFloat();
m=km*0.621371;
System.out.println("This is equal to: "+m);
}
}
It gives me an error saying:
Incompatible types:possible lossy conversion from double to float.
You need to define constant variable as a float, since km is read as a float.
The value
0.621371
is adouble
literal, so thekm
values is promoted todouble
when multiplied. Storing thedouble
product back tom
would be a conversion that could lose data (double
tofloat
).To keep the data as a
float
, use afloat
literal, with af
on the end:Normally a
double
for the results would be just fine, so you could also just change the datatypes ofm
andkm
todouble
.You are trying to set a
double
to afloat
variableTo fix, change this line
to