I've created a class called Human
that works properly, it takes 2 arguments, age and name, to create a human.
To create a Human with command line arguments, I want to write
java Filename -H "Anna" 25
And it should create a Human with thatname="Anna"
, and thatage=25
, where 25 is an int.
The code I've written is creating a list called Argument, and iterating through it to find -H
. I need to do this because I'm going to be using this to create different classes later. I just need help with the syntax on the lines where I've written thename=
, and theage=
, because I don't know how to get the next item in list
, and 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);
}