1

I'm trying to display an error message if the user supplies no command line arguments. I'm getting an error when I type "java sheet12t1" into cmd. I have the rest of my program nailed down, it's just that this part won't work.

The question I'm working with is below, along with my code.

This is the question I'm working with: https://i.stack.imgur.com/YNMJF.png

public class sheet12t1
{
    public static void main(String[] args)
    {
        int projectCategory = Integer.parseInt(args[0]);
        int keyServiceType = Integer.parseInt(args[1]);
        if (args.length != 2)
            System.out.println("Please enter: java sheet12t1 projectCategory serviceType");

        else if (args.length == 0)
            System.out.println("Please enter two valid inputs.");

        else if (projectCategory < 1 || projectCategory > 3)
            System.out.println("Please enter valid inputs.");

        else if (keyServiceType < 1 || keyServiceType > 3)
            System.out.println("Please enter valid inputs.");

        else
        {
            double result = calculateProjectCost(projectCategory, keyServiceType);
            System.out.println("Total project cost: " + result);
        }
    }

    public static double calculateProjectCost(int projectCategory, int keyServiceType)
    {
        double totalCost = 10000;
        if (projectCategory == 1)
        {
            if (keyServiceType == 1)        totalCost += 5600.5;
            else if (keyServiceType == 2)   totalCost += 5500;
            else                            totalCost += 6000;
        }
        else if (projectCategory == 2)
        {
            if (keyServiceType == 1)        totalCost += 10600.5;
            else if(keyServiceType ==2)     totalCost += 11000;
            else                            totalCost += 13500;
        }
        else
        {
            if (keyServiceType == 1)        totalCost += 17000;
            else if(keyServiceType == 2)    totalCost += 22000;
            else                            totalCost += 25000;
        }
        return totalCost;
    }
}
JamesR
  • 113
  • 1
  • 1
  • 8

2 Answers2

3

you need to check the length of the arguments array before you start using it and exit if it is wrong, change:

    int projectCategory = Integer.parseInt(args[0]);
    int keyServiceType = Integer.parseInt(args[1]);

    if (args.length != 2)
        System.out.println("Please enter: java sheet12t1 projectCategory serviceType");

to

    if (args.length != 2) {
        System.out.println("Please enter: java sheet12t1 projectCategory serviceType");
        System.exit(1);
    }

    int projectCategory = Integer.parseInt(args[0]);
    int keyServiceType = Integer.parseInt(args[1]);

taken from https://stackoverflow.com/a/2670980/621366 a short explanation why System.exit(0) or System.exit(1)

The "0" lets whomever called your program know that everything went OK. If, however, you are quitting due to an error, you should System.exit(1);, or with another non-zero number corresponding to the specific error.

Community
  • 1
  • 1
peter
  • 14,348
  • 9
  • 62
  • 96
  • This will only print out `Please enter...` and then throw the exception if `args.length != 2`, the `if` block should also contain `System.exit(1);` to ensure exception isn't thrown – Ian2thedv Nov 20 '14 at 10:42
0

Try to do something like this :

public static void main(String[] args)
{
  System.out.println(args.length);
}

you can place a check whether args.length is 0 or not!

Sarthak Mittal
  • 5,794
  • 2
  • 24
  • 44