How do I input integers into a program?
This code will take the number I give it, but then it outputs the sum completley wrong.
public class gallonstolitres
{
public static void main(String[] args) throws Exception
{
char gallons;
System.out.println("how many gallons do you want to be converted?");
gallons = (char)System.in.read();
System.in.read();
System.out.println(gallons+" gallons is "+gallons*4.5+" litres");
}
}
I assume I need to change the char gallons to int gallons, otherwise it uses the int value for the keyboard char I pressed and not the actual number I typed in? How do I do this?
This code will take the number I give it, but then it outputs the sum completley wrong.
public class gallonstolitres
{
public static void main(String[] args) throws Exception
{
char gallons;
System.out.println("how many gallons do you want to be converted?");
gallons = (char)System.in.read();
System.in.read();
System.out.println(gallons+" gallons is "+gallons*4.5+" litres");
}
}
I assume I need to change the char gallons to int gallons, otherwise it uses the int value for the keyboard char I pressed and not the actual number I typed in? How do I do this?
Last edited: