命令行参数
Command line arguments
我创建了一个名为 Human
的 class,它可以正常工作,它需要 2 个参数,年龄和名字,来创建一个人。
要创建一个带有命令行参数的人,我想写
java Filename -H "Anna" 25
它应该创建一个具有 thatname="Anna"
和 thatage=25
的 Human,其中 25 是一个整数。
我编写的代码创建了一个名为 Argument 的列表,并遍历它以找到 -H
。我需要这样做,因为稍后我将使用它来创建不同的 classes。我只需要有关我编写 thename=
和 theage=
行的语法方面的帮助,因为我不知道如何获取 next item in list
和 next-next item in list
.
public static void main(String[] args) {
ArrayList<String> Argument = new ArrayList<String>();
for (String item: args) {
Argument.add(item);
}
for (String item2: Argument) {
if (item2 == "-H") {
thatname = Argument.get(item2+1)
thatage = Argument.get(item2+2)
Human person = new Human(thatname,thatage);
System.out.println(person);
}
既然可以直接使用args,为什么还要使用array list?虽然你只有两个参数,但不要使用 for 循环直接访问它们。
你应该知道的一件事是 String 是 java 中的一个对象,所以比较两个带有 ==
符号的字符串将 return false 即使它们具有相同的值(== 将比较object),你应该使用.equale()
函数来比较object的值。
查看此代码:
public static void main(String[] args) {
if(args.length>0)
{
try{
if (args[0].equals("-H")) {
Human person = new Human( args[1],args[2]);
System.out.println(person);
}
}catch(ArrayIndexOutOfBoundsException exception) {
System.out.println("error with parameters");
}
}else
System.out.println("No command");
}
为什么不循环参数?
for ( int i = 0; i < args.length; i++ ) }
if (args[i].equals("-H")) {
i++;
thatName = args[i];
i++;
thatAge = args[i];
}
}
如果有人不遵守您设置的规则,您应该添加一些代码来捕获。可能没有足够的参数或人类在键盘上做的其他事情...
thatname = Argument.get(Argument.indexOf(item2)+1);
thatage = Argument.get(Argument.indexOf(item2)+2);
或者你知道第一个元素是-H
,第二个元素是name
,第三个元素是age
,所以你可以直接使用下面的代码
thatname = Argument.get(1);
thatage = Integer.parseInt(Argument.get(2));
你的代码有问题,我给你解释一下,希望对你有所帮助。
- 您不能将字符串与 == 进行比较,您必须使用 equals 方法
在字符串中比较两个字符串。
- 你必须使用这个 for 循环 for(int i = 0; i < Argument.size();
i++) 语法,以便您可以从零迭代到
列表中的项目。
ArrayList 中的 - get 方法将索引作为参数,return 该索引处的值。
- 您可以添加 i += 2 以跳过接下来的两次迭代
return 人的姓名和年龄值。 (可选)
这是工作代码:
public static void main(String[] args) {
ArrayList<String> Argument = new ArrayList<String>();
for (String item: args) {
Argument.add(item);
}
String currentItem;
for (int i = 0; i < Argument.size(); i++) { // it will iterate through all items
currentItem = Argument.get(i); // getting the value with index;
if (currentItem.equals("-H")) {
String thatname = Argument.get(i+1);
String thatage = Argument.get(i+2);
i += 2; // Skipping 2 iterations of for loop which have name and age human.
Human person = new Human(thatname,thatage);
System.out.println(person);
}
}
}
您不能使用 String
迭代列表。迭代列表时,您需要一个索引号,例如 list.get(indexNumber);
public static void main(String[] args) {
ArrayList<String> Argument = new ArrayList<String>();
for (String item: args) {
Argument.add(item);
}
for (int i=0;i<args.length;i++) {
if (args[i].trim().equals("-H")) {
i++;
thatname = Argument.get(i);
i++;
thatage = Argument.get(i);
Human person = new Human(thatname,thatage);
System.out.println(person.getName()+" "+person.getAge());
}
我创建了一个名为 Human
的 class,它可以正常工作,它需要 2 个参数,年龄和名字,来创建一个人。
要创建一个带有命令行参数的人,我想写
java Filename -H "Anna" 25
它应该创建一个具有 thatname="Anna"
和 thatage=25
的 Human,其中 25 是一个整数。
我编写的代码创建了一个名为 Argument 的列表,并遍历它以找到 -H
。我需要这样做,因为稍后我将使用它来创建不同的 classes。我只需要有关我编写 thename=
和 theage=
行的语法方面的帮助,因为我不知道如何获取 next item in list
和 next-next item in list
.
public static void main(String[] args) {
ArrayList<String> Argument = new ArrayList<String>();
for (String item: args) {
Argument.add(item);
}
for (String item2: Argument) {
if (item2 == "-H") {
thatname = Argument.get(item2+1)
thatage = Argument.get(item2+2)
Human person = new Human(thatname,thatage);
System.out.println(person);
}
既然可以直接使用args,为什么还要使用array list?虽然你只有两个参数,但不要使用 for 循环直接访问它们。
你应该知道的一件事是 String 是 java 中的一个对象,所以比较两个带有 ==
符号的字符串将 return false 即使它们具有相同的值(== 将比较object),你应该使用.equale()
函数来比较object的值。
查看此代码:
public static void main(String[] args) {
if(args.length>0)
{
try{
if (args[0].equals("-H")) {
Human person = new Human( args[1],args[2]);
System.out.println(person);
}
}catch(ArrayIndexOutOfBoundsException exception) {
System.out.println("error with parameters");
}
}else
System.out.println("No command");
}
为什么不循环参数?
for ( int i = 0; i < args.length; i++ ) }
if (args[i].equals("-H")) {
i++;
thatName = args[i];
i++;
thatAge = args[i];
}
}
如果有人不遵守您设置的规则,您应该添加一些代码来捕获。可能没有足够的参数或人类在键盘上做的其他事情...
thatname = Argument.get(Argument.indexOf(item2)+1);
thatage = Argument.get(Argument.indexOf(item2)+2);
或者你知道第一个元素是-H
,第二个元素是name
,第三个元素是age
,所以你可以直接使用下面的代码
thatname = Argument.get(1);
thatage = Integer.parseInt(Argument.get(2));
你的代码有问题,我给你解释一下,希望对你有所帮助。
- 您不能将字符串与 == 进行比较,您必须使用 equals 方法 在字符串中比较两个字符串。
- 你必须使用这个 for 循环 for(int i = 0; i < Argument.size(); i++) 语法,以便您可以从零迭代到 列表中的项目。 ArrayList 中的
- get 方法将索引作为参数,return 该索引处的值。
- 您可以添加 i += 2 以跳过接下来的两次迭代 return 人的姓名和年龄值。 (可选)
这是工作代码:
public static void main(String[] args) {
ArrayList<String> Argument = new ArrayList<String>();
for (String item: args) {
Argument.add(item);
}
String currentItem;
for (int i = 0; i < Argument.size(); i++) { // it will iterate through all items
currentItem = Argument.get(i); // getting the value with index;
if (currentItem.equals("-H")) {
String thatname = Argument.get(i+1);
String thatage = Argument.get(i+2);
i += 2; // Skipping 2 iterations of for loop which have name and age human.
Human person = new Human(thatname,thatage);
System.out.println(person);
}
}
}
您不能使用 String
迭代列表。迭代列表时,您需要一个索引号,例如 list.get(indexNumber);
public static void main(String[] args) {
ArrayList<String> Argument = new ArrayList<String>();
for (String item: args) {
Argument.add(item);
}
for (int i=0;i<args.length;i++) {
if (args[i].trim().equals("-H")) {
i++;
thatname = Argument.get(i);
i++;
thatage = Argument.get(i);
Human person = new Human(thatname,thatage);
System.out.println(person.getName()+" "+person.getAge());
}