Java main 方法参数处理,取决于用户输入

发布于 2024-12-26 06:45:22 字数 538 浏览 1 评论 0原文

我想知道如何比较传递给 java main 方法的参数。

eg java hello -i

我尝试打印 args[0] ,它确实给了我 -i 。但是我想要实现的是:

if args.length == 0 {
  do something
}
else if args[0] =="-i"{
  do something
}

但是我不断收到索引越界异常。无论如何,是否可以将字符串数组 args 中的字符串转换为字符串类型,以便我可以比较它?

public static void main(String[] args) {
  if args.length == 0 {
    do something
  }
  else if args[0] =="-i"
    do something
  } 


}

例如,如果他们在没有任何参数的情况下启动程序,我将调用 init() 方法。但如果他们输入 -i 作为参数,我将调用 install() 。

I would like to know to know how do i compare the arguments passed into a java main method.

eg java hello -i

I tried printing args[0] and it does indeed gives me -i. however what i want to achieve is:

if args.length == 0 {
  do something
}
else if args[0] =="-i"{
  do something
}

However i keep getting index out of bound exception. is there anyway to convert the string in the string array args to just a string type so i can compare it?

public static void main(String[] args) {
  if args.length == 0 {
    do something
  }
  else if args[0] =="-i"
    do something
  } 


}

for example if they start the program without any arguments, i will call init() method. but if they entered -i as arguments, i will call install() instead..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

内心旳酸楚 2025-01-02 06:45:22

您给出的代码甚至无法编译,但这确实可以工作:

public class Test {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("No arguments!");
        } else if (args[0].equals("-i")) {
            System.out.println("-i passed");
        } else {
            System.out.println("Something else");
        }
    }
}

请注意,在这里使用else很重要 - 这会失败,例如:

if (args.length == 0) {
    System.out.println("No arguments!");
}
if (args[0].equals("-i")) {
    System.out.println("-i passed");
}

此时,即使数组的长度为 0,您也会检查 args[0] 。鉴于您目前给出了伪代码(条件周围没有括号),我想知道这是否是您真实代码中的问题。

(另请注意使用 equals 而不是 ==,正如其他人已经指出的那样。)

The code you've given wouldn't even compile, but this does work:

public class Test {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("No arguments!");
        } else if (args[0].equals("-i")) {
            System.out.println("-i passed");
        } else {
            System.out.println("Something else");
        }
    }
}

Note that it's important that you're using else here - this would fail, for example:

if (args.length == 0) {
    System.out.println("No arguments!");
}
if (args[0].equals("-i")) {
    System.out.println("-i passed");
}

at that point you're checking args[0] even if the length of the array is 0. Given that you've given pseudo-code at the moment (no brackets round the conditions) I wonder whether that's the problem in your real code.

(Also note the use of equals instead of == as others have already pointed out.)

枕梦 2025-01-02 06:45:22

比较字符串时,您需要使用 equals() 方法,因为 == 比较它们是否是相同的字符串(与相等不同)。

这是一个示例,

public static void main(String[] args) {
    if (args.length == 0) {
        System.err.println("No arguments");
    } else if (args.length == 1) {
        if (args[0].equals("first")) {
            System.err.println("The argument equals 'first'");
        } else {
            System.err.println("Don't know what you want with " + args[0]);
        }
    } else {
        System.err.println("Will not use the arguments");
        for (String arg: args) {
            System.err.println(arg);
        }
    }
}

尝试在不带参数、第一个参数或任何其他参数的情况下调用它。

When comparing strings you need to use the equals() method as the == compare if they are the same string (not same as if they are equal).

Here's an example

public static void main(String[] args) {
    if (args.length == 0) {
        System.err.println("No arguments");
    } else if (args.length == 1) {
        if (args[0].equals("first")) {
            System.err.println("The argument equals 'first'");
        } else {
            System.err.println("Don't know what you want with " + args[0]);
        }
    } else {
        System.err.println("Will not use the arguments");
        for (String arg: args) {
            System.err.println(arg);
        }
    }
}

Try calling it with no arguments, the argument first or any other arguments.

旧瑾黎汐 2025-01-02 06:45:22

有没有办法将字符串数组参数中的字符串转换为字符串类型,以便我可以比较它?

你不需要转换它。但是,您确实需要以正确的方式进行比较;例如

if (args.length == 0) {
   // do something
} else if (args[0].equals("-i")) {
   // do something else 
}

(如果您使用 == 来比较字符串,您可能会遇到麻烦。== 运算符测试两个引用是否指向同一个对象。什么您实际上需要的是查看两个字符串是否包含相同的字符...并且您需要使用 equals 方法来执行此操作。)


但是,这并不能解释为什么您会得到 IndexOutOfBoundsException。我需要查看真实代码来回答这个问题。

Is there anyway to convert the string in the string array args to just a string type so I can compare it?

You don't need to convert it. However, you do need to compare it the right way; e.g.

if (args.length == 0) {
   // do something
} else if (args[0].equals("-i")) {
   // do something else 
}

(If you use == to compare strings, you are likely to get into trouble. The == operator tests to see if two references point to the same object. What you actually need is to see if two strings contain the same characters ... and you need to use the equals method to do that.)


However, this doesn't explain why you were getting IndexOutOfBoundsException. I'd need to see real code to answer that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文