ArrayIndexOutOfBoundsException when launching a java program

前端 未结 2 1825
逝去的感伤
逝去的感伤 2020-12-07 05:20

I am currently working on an assignment but there seems to be a problem when running my code.

public class caesar {
    public static void main(String args[         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 06:04

    You are not passing command line arguments to your program and don't check whether they are passed. To pass arguments launch your program like

    java caesar arg0 arg1
    

    For example:

    java caesar somestring 10
    

    To do this in NetBeans 8.0.2 IDE, open Project Properties, select the Run item, then specify the arguments there:

    You may probably also want to check the number of passed arguments in advance to output the friendly error message. For example:

    public static void main(String args[]) {
        if(args.length != 2) {
            System.err.println("Usage: java caesar  ");
            return;
        }
        ... // the rest of your code
    }
    

提交回复
热议问题