0

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);
        }
armara
  • 535
  • 3
  • 17
  • [How do I compare Strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) Also, what should the result of the addition of String and integer be? An index? How? – jp-jee Nov 09 '17 at 11:52
  • What do you mean? Where am I adding a string with an integer? The part where I've written `Argument.get(item2+1)`, is not me trying to add an integer and a string, I know this is incorrect coding. I'm just trying to explain in the code that I'm trying to find the value that's after `item2`. I don't know the syntax for that. Also, I know that `get` doesn't work for me, because `get` only works for `ints`. So really what I'm asking for, is the syntax to `get` the `String` in `Argument`that's after item2 for `thatname`, and the one that's after that for `thatage`. – armara Nov 09 '17 at 12:02

5 Answers5

1

Why not just loop the args?

for ( int i = 0; i < args.length; i++ ) }
    if (args[i].equals("-H")) {
       i++;
       thatName = args[i];
       i++;
       thatAge = args[i];
    }
}

You should add some code to catch if one does not follow the rules you have set. Probably not enough arguments or other things humans do at the keyboard...

Fredy Fischer
  • 458
  • 3
  • 12
  • I tried using this, but I just get an empty output. `for ( int i = 0; i < args.length; i++ ) { if (args[i].equals("-H")) { i++; String thatName = args[i]; i++; int thatAge = Integer.parseInt(args[i]); Human person = new Human(thatAge,thatName); System.out.println(person); } }` – armara Nov 09 '17 at 12:16
  • I think, person is not printable. Try to implement a toString() method. public String toString() { return getName() + " " + Integer.toString(getAge()); } – Fredy Fischer Nov 09 '17 at 13:00
1
thatname = Argument.get(Argument.indexOf(item2)+1);
thatage = Argument.get(Argument.indexOf(item2)+2);

OR you know that 1st element is -H, 2nd element is name and 3rd is age, so you can use below code directly

thatname = Argument.get(1);
thatage = Integer.parseInt(Argument.get(2));
sanjeevjha
  • 1,439
  • 14
  • 18
1

Your code have some problems, let me explain them to you for your assistance.

  1. You cannot compare Strings with == you have to use equals method in the String to compare between two Strings.
  2. You have to use this for loop for(int i = 0; i < Argument.size(); i++) syntax so that you can iterate from zero to the number of items in the list.
  3. get method in ArrayList take the index as parameter and return the value at that index.
  4. You can add i += 2 to skip the next two iterations which will return the name and age value of the human. (It is optional)

Here is the working code:

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);

        }

    }
}
Rehan Javed
  • 418
  • 3
  • 14
0

Why using array list when you can directly use args? and while you're having only two parameters don't use a for loop access them direclty. One thing you should know is that String is an object in java so comparing two Strings with == sign will return false even if they have same value (== will compare the id of the object), you should use .equale() function to compare the value of the object.

Check out this code :

 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");
      }
mr.infin8
  • 53
  • 1
  • 1
  • 5
0

You cannot iterate the list using String. When iterating a list you required a index number like 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());
        }