Me again. I have to write a program that converts Fahrenheit to Celsius and vice versa using the commande line for school. I am very new to code and I have trouble location my problem.
I have this code
public class Temperature {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++)
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
System.out.println ("Veuillez specifier c (celsius) ou f (fahrenheit) suivi de la température. Exemple argc arg32");
if (args[0].equals ("c"))
{
/*convertir en fahrenheit*/
double z = (1.8 * b) + 32;
System.out.print ("La température est" + z);
}
else if (args[0].equals ("f"))
{
/*convertir en celsius*/
double y = (b - 32)/1.8;
System.out.print("La température est" + y);
}
}
}
}
My idea was to use to command line to first choose the unit then the temperature like so
java Temperature c 35
But I get a bunch of errors when I try it. My guess being that I can't use in string in the command line? c being a string?
I get the errors
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInput.String(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Temperature.main(Temperature.java:12)
I did not get any error while compiling though
forloop?forloop. Since you know you'll have two arguments, the first being the units and the second being the temperature, you don't need a loop since you access the arguments directly.