-1

I'm learning using an Array and Command line argument for Java. We write a program that convert from Celsius degree to Fahrenheit degree. I started a basic idea for my project but not sure I did it right (I'm beginner).

public class Implementation 
{
    public static void main(String[] args)
    {
        {
        String[] days = new String[7];
        days[0] = "Very Cold";
        days[1] = "Cold";
        days[2] = "Mild";
        days[3] = "Very Mild";
        days[4] = "Warm";
        days[5] = "Very Warm";
        days[6] = "Hot";
        }
        if (args.length != 5)
        {
            System.out.println("Error! Please try again.");
            System.exit(0);
        }
        else
        {
            String name;
            String convert;
            double degree;
            String celsius;
            String fahrenheit;

            name = args[0];
            convert = args[1];
            degree = Double.parseDouble(args[2]);
            celsius = args[3]; 
            fahrenheit = args[4];         

            System.out.printf("%n%s Celsius is %.2f Fahrenheit\n", args[2], ( 9.0 / 5.0 *  (degree + 32)));
        }
    }
}

When I supply my command line argument, it should be in this form:
Java TempConversion 100 c f

My questions are: Did I do my command line arguments right? It seem like I did something wrong even though I still have the same output. How do I show up my array in the output with specific degree? Follow this:

Below 0 degrees = Very cold

From 0 to 32 = Cold

From 33 to 50 = Mild

From 51 to 60 = Very mild

From 61 to 70 = Warm

From 71 to 90 = Very warm

Above 90 = Hot

Jin Nguyen
  • 45
  • 10
  • What do you expect the output of the program to be if you execute `java TempConversion 100 c f`? I'd expect it to convert Celsius to Fahrenheit just from a glance, but that's not what your program will do at all. – Makoto Mar 16 '15 at 21:50
  • Why do you read in `Java`, `TempConversion`, `c` and `f` from the command line if you only use the actual degree number? – Smutje Mar 16 '15 at 21:50
  • I do not get command line argument very well. Can you show me what exactly I need to do here? Please! – Jin Nguyen Mar 16 '15 at 21:53

2 Answers2

2

A couple of things:

  • "Java" and the program name (TempConversion) aren't arguments. Everything after that is. So when you put args.length != 5 you weren't counting the program name and the java keyword. You should use args.length != 3 (as opposed to 5).

    • e.g. The arguments (in the example) are {"100", "c", "f"} with the assignments 0, 1, 2.

Edit:

A question was asked as to how to get the program name and java keyword.

  • The java keyword is a constant. That is you can use "Java" for all programs.
  • To find the class name you can see this post. Here is my implementation. Should work.

  • This likely won't cause you much grief, but I don't see a reason for those brackets starting around String[] days and ending at day[6] =...; " }". There isn't a need for them and String[] can have elements that are set and used anywhere (No braces required). Everything else looks fine.

Community
  • 1
  • 1
lacraig2
  • 655
  • 4
  • 18
  • How do I suppose to do if I want "Java" and "Temp Conversion" to be a part of arguments? – Jin Nguyen Mar 16 '15 at 21:58
  • @Jin "Java" (which should be "java" lowercase j) the invoking program and "TempConversion" **aren't** arguments, and you can't make them arguments. – Stephen P Mar 16 '15 at 22:00
  • @Jin I answered your question. – lacraig2 Mar 16 '15 at 22:03
  • Now I get what it mean, thank you very much. So know where should I put my function "9.0 / 5.0 * (degree + 32)" Because I think it does not suppose to be there. – Jin Nguyen Mar 16 '15 at 22:08
1

In java TempConversion 100 c f your command line arguments are 100 and c and f -- there are three arguments, but you are checking for five arguments in if (args.length != 5) This is unlike C where argv[0] is the program name itself.

You can see this with a minimal program

public class c2f
{
    public static void main(String[] args)
    {
        System.out.println("args.length is " + args.length);

        for (int i=0; i<args.length; i++) {
            System.out.println("arg[" + i + "] is " + args[i]);
        }
    }
}

Run this as

javac c2f.java
java c2f 100 c f

Your output should be

args.length is 3
arg[0] is 100
arg[1] is c
arg[2] is f

You are also not looking at either the "c" or the "f" arguments. Maybe you intend to add to your program so those tell you if you want to do a C->F or a F->C conversion.

Stephen P
  • 14,422
  • 2
  • 43
  • 67
  • I get the `(args.length != 3)` now. But where should I put my function `9.0 / 5.0 * (degree + 32)` in the program? And If I do conversion the other way (F -> C). How should I do? Can I do the overloading? – Jin Nguyen Mar 16 '15 at 22:14