Accepting a positive integer from either a command-line argument or System.in I've been doing a lot of programs for classes that require a prompt from the program. I thought it would be much faster for me to rapidly test inputs if I didn't have to execute the program and then type the input once inside the program (as opposed to just hitting ↑ and adjusting the input that way).

My concern is this code seems to have some duplication with the exception handling, and I'm also not sure if it's as efficient as it can be. I'm somewhat of a beginner, but I'm certainly interested in more advanced ideas/solutions as that would point in direction of where to self-study more.

import java.util.Scanner;

/*
 *  This program is to implement testing command line input or reverting to regular prompts if
 *  an argument isn't provided or is invalid. It tests to see if the input is a negative integer,
 *  and displays the result.
 *
 *  Algorithm:
 *  If the console argument is valid, treat it as user input, or otherwise ignore it
 *  Continue to ask for a positive integer until one is given.
 *  Display the integer given and confirm it's negative
 */
public class TestConsoleArgs
{
    public static void main( String[] args )
    {
        String arg;
        Scanner user_input = new Scanner(System.in);
        int num = 0;

        try {
            arg = args[0];
            num = Integer.parseInt(arg);
        } catch (ArrayIndexOutOfBoundsException e) {
            arg = "";
        } catch (NumberFormatException e2) {
            arg = "";
        }

        while (num &lt;= 0)
        {
            System.out.print("Please type in a positive integer.  ");
            arg = user_input.next();

            try {
                num = Integer.parseInt(arg);
            } catch (NumberFormatException e) {
                System.out.print("That's not an integer! ");
                continue;
            }

            if (num &lt;= 0)
            {
                System.out.print("That's a negative number. ");
            }
        }


        System.out.println("Yes, '" + num + "' is a positive integer.");
    }
}
user_input can be better renamed as userInput.

If you want to make the prompting 'self-contained', put it inside a method that will re-prompt the user for non-integer values:

private static int getIntegerInput(Scanner scanner) { ... }


I also don't think you need to catch ArrayIndexOutOfBoundsException, instead consider picking up from the command line only if args.length is at least 1:

// calls getIntegerInput(Scanner) with scanner if input is not an integer
private static int parseStringOrPrompt(String input, Scanner scanner) { ... }


Putting it altogether:

public static void main(String[] args) {
    try (Scanner scanner = new Scanner(System.in)) {
        int num = args.length &gt; 0 ? parseStringOrPrompt(args[0], scanner) 
                                    : getIntegerInput(scanner);
        while (num &lt; 0) {
            System.out.print("That's a negative integer.");
            num = getIntegerInput(scanner);
        }
        System.out.println("Yes, '" + num + "' is a positive integer.");
    }
}