Home>

Run the Java file with cmd.
If i have two arguments and either of them is not an integer,
The corresponding argument is displayed in the "a" part, such as "Enter an integer: a" as an error message.
Assuming that i am passing two arguments, if only one of them is not an integer, one sentence is sufficient as above, but if neither is an integer, the above sentence is displayed in two lines.
Please tell me how to get the corresponding argument when jumping to an exception with try-catch as shown in the code below.
Or if you can only do it by other methods, please tell me how to do it.
Thank you.

Kadai1.java

public class Kadai1 {
    public static void main (String [] args) {
        int num1 = 0;
        int num2 = 0;
        try {
            num1 = Integer.parseInt (args [0]);
            num2 = Integer.parseInt (args [1]);
        } catch (ArrayIndexOutOfBoundsException e) {
           /* Argument existence check * /
            System.out.println ("Please keep the number of command line arguments to two.");
        } catch (NumberFormatException e) {
           /* Argument integer check * /
            // I want to display the corresponding argument in the a part of the following message
            System.out.println ("Enter an integer:" + a);
        }
    }
}
  • Answer # 1

    Based on the advice of dodox86, I created the following code and executed it without any error.

    public class Kadai1 {
        public static void main (String [] args) {
            int num1 = 0;
            int num2 = 0;
            int check = 0;
            final int size = 2;
            int leng = args.length;
            if (leng! = size) {
                System.out.println ("Please keep the number of command line arguments to two.");
                System.exit (0);
            }
           /* args [0] integer check * /
            try {
                num1 = Integer.parseInt (args [0]);
            } catch (NumberFormatException e) {
               /* Argument integer check * /
                System.out.println ("Enter an integer:" + args [0]);
                check = 1;
            } finally {
                try {
                   /* args [1] integer check * /
                    num2 = Integer.parseInt (args [1]);
                    if (check == 1) {
                        System.exit (0);
                    }
                    int sum = num1 + num2;
                    System.out.println (The sum of args [0] + "and" + args [1] + "is" + sum + ".");
                } catch (NumberFormatException e) {
                   /* Argument integer check * /
                    System.out.println ("Enter an integer:" + args [1]);
                }
            }
        }
    }

  • Answer # 2

    If the size of the array of args is less than 2. >Display an error and exit

    Check if args [0] is a numerical expression>If false, error display

    Check if args [1] is a numerical expression>If false, error display

    If any of the above is an error, exit as is

    Any production processing?

    Let's go with a code like this

  • Answer # 3

    Yup?
    You should write it in the place where you are catching NumberFormatException when an exception normally flies.

    The exception is, if you want to get it and process it, you should make it a method.

    You know how to write a method, right?
    If you don't know, look it up.

      // Assuming in class
        static public int [] inputEx (String [] args) {
            int [] res = new int [2];
            try {
                // todo: Convert to integer here
            } catch (NumberFormatException e) {
                // todo: Get here by standard input
            }
        return res;
        }

    Or something like that, get it with this. And.

    It may be better not to be static if possible.

  • Answer # 4

    Only one exception can be caught with one try-catch.
    Therefore

    If neither is an integer, the above sentence is displayed in two lines.

    Cannot be achieved.

    Perform integer checking to prevent exceptions

    Add two exceptions (whether to write twice or make it a function)

    You need to change the code like this.