0

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

4
  • your first argument is a char but you are using parseInt? Commented May 14, 2014 at 1:00
  • Out of curiosity - why the for loop? Commented May 14, 2014 at 1:08
  • @IdanArye I didn't know what else to use Commented May 16, 2014 at 19:59
  • You don't need any loop here. If you had an unknown number or command line arguments and you wanted to convert them all to numbers you could use a for loop. 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. Commented May 16, 2014 at 20:19

3 Answers 3

3
int a  = Integer.parseInt(args[0]);

this is giving you the exception, you are trying to parse a String to an Integer

change it to

String unitChar  = args[0];

Also, once you have read argument into a variable, use it for comparison rather than accessing it again from arguments

e.g.

if (unitChar.equals("c"))
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not very familia with using comparison rather than accessing a variable. What does that imply exactly?
when you say args[0].equals(something) it will look into array again and again while if you use unitChar it will not have to look into args array every time
0
int a  = Integer.parseInt(args[0]);

Is the line that is giving you the error. You are trying to parse an int out of a string that is not a number. You don't seem to use a in your code, so I'd remove it. If you do still need it, just do as suggested in VD's answer and use this line instead:

String unitChar  = args[0];

5 Comments

It's just that the homework said that I should refer to the previous exercises to write the program and the previous exercice had the int a and int b bit already written (I had to complete the code) in the code so I figured I could do the same in this exercice
I see. They must have assumed you'd change that line I think because c and f can't be parsed as integers. Does your code work by removing that line?
That just means it's storing unitchar as a string instead of an int
I didn't try yet I was trying to understand String unitChar = args[0];. I get what String and Char are as well as args[0] but I don't get the unit before the Char. I tried to google it but it kept bringing up UniChar and not unitChar
Thats the name of the variable. It can be whatever you want
0

Below is how I would write it...

The for loop you had looped through the code twice, I'm assuming you wanted to check the length of the String[].

Next I took the first arugment String[0] and called the toLowerCase method. This is for the user can either enter 'C' or 'c' or 'F' or 'f'.

Next I changed your if and else if statements to a switch statement. This is more personal perference, but it stops you from writing a ton of else if statements. Also the default clause is if the user enters a character that is not 'c' or 'f' you can alert the user.

The System.exit statements are really up to you, but when you need to write larger programs it's good to exit the program with an error code so you can track it back later. System.exit(0) is used normally as the program worked as desired.

public  class Temperature 
{
    public  static void main(String[] args) 
    {
        if (args.length == 2)
        {
            String a  = args[0].toLowerCase();
            int b  = Integer.parseInt(args[1]);

            System.out.println ("Veuillez specifier c (celsius) ou f (fahrenheit) suivi de la température. Exemple argc arg32");

            switch(a)
            {
                case: "c"
                    /*convertir en fahrenheit*/
                    double z = (1.8 * b) + 32;
                    System.out.print ("La température est" + z);
                    break;

                case: "f"
                    /*convertir en celsius*/
                    double y = (b - 32)/1.8;
                    System.out.print("La température est" + y);
                    break;

                default:
                    System.out.println("the first argument needs to be 'c' for celsius or 'f' for fahrenheit");
                    System.exit(-1);

            }
        }
        else
        {
            System.out.println("Not enough arguments passed in.");
            System.exit(-1);
        }
        System.exit(0);
    }       
}

Comments

Your Answer

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.