简体   繁体   中英

Command line arguments calculation

I need to write a program that using command line arguments. I'm a beginner in java. The program needs three command-line arguments, see following:

  1. The first number
  2. The operation (+ , - , *, / )
  3. The second number

If error occurs, display a message. If not, display the answer. ("This answer is").

Ex: 5 + 3

Here is what I have done:

import java.util.Arrays;

public class math

{

public static void main (String args[])

    {         
        int firstNumber = Integer.parseInt(args[1]);

        int secondNumber = Integer.parseInt(args[2]);

        char theOperator = args[3].charAt(0);

        int result = 0;

        System.out.print(args[1] + " " + args[3] + " " + args[2] + " = " );

        switch (theOperator)
        {
            case ('+'):
                result = firstNumber + secondNumber; 
                break;
            case ('-'):
                result = firstNumber - secondNumber; 
                break;
            case ('*'):
                result = firstNumber * secondNumber; 
                break;
            case ('/'):
                result = firstNumber / secondNumber; 
                break;
            default:
                System.out.println("Invalid Operator selected");
        }
        if (args.length != 3)
            System.out.println("Error, Please try again!");
        else
        {
            System.out.printf("This answer is ", result);
        }
    }
}

I don't know what I am doing wrong. The program does not run. Anyone can help me? Please!

Array index starts at 0. Ex:

int firstNumber = Integer.parseInt(args[0]); //Correct version

You should have decremented all your index refernces by 1 and fixed the order:

int firstNumber = Integer.parseInt(args[0]);
char theOperator = args[1].charAt(0); //Wrong order here asusming its comming in as 1 + 2. Also charAt should be 0 indexed as well.
int secondNumber = Integer.parseInt(args[2]);

First you need to control if args has 3 elements. Also arrays are zero indexed, therefore you need to use args[0],args 1 and args[2] to use arguments.

public static void main (String args[]) {   
    if (args.length != 3) {
        System.out.println("This program needs 3 arguments: firstNumber, secondNumber and operator");
        return;
    } 
    int firstNumber = Integer.parseInt(args[0]);
    int secondNumber = Integer.parseInt(args[1]);
    char theOperator = args[2].charAt(0);


    //.... 
}

Also you need to give command line arguments when you are running from IDE. Yours is netbeans therefore use following tutorial to set command line arguments.

Like Eric said, you need to start the arrays at 0.

However, since you say you get and OutOfBoundsException at index 0, most likely nothing is being passed as arguments from the command line. I don't work with the cmd much, but I assume that you are making some mistake when entering in the three arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM