Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0

前端 未结 3 1050
一个人的身影
一个人的身影 2021-01-16 15:39
public class TestSample {
    public static void main(String[] args) {
        System.out.print(\"Hi, \");
        System.out.print(args[0]);
        System.out.prin         


        
3条回答
  •  醉话见心
    2021-01-16 16:22

    with regards to second part of your question:

    from http://download.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html :

    Parsing Numeric Command-Line Arguments

    If an application needs to support a numeric command-line argument, it must convert a String argument that represents a number, such as "34", to a numeric value. Here is a code snippet that converts a command-line argument to an int:

    int firstArg;
    if (args.length > 0) {
        try {
            firstArg = Integer.parseInt(args[0]);
        } catch (NumberFormatException e) {
            System.err.println("Argument must be an integer");
            System.exit(1);
        }
    }
    

提交回复
热议问题