Mar
Mar

Reputation: 35

Java ArrayList strings lengths printing

My excercise is:

Create the method lengths that gets a list of String variables as a parameter and returns an ArrayList that contains the lengths of the Strings in the same order as the original list.

And my code:

import java.util.ArrayList;

public class Test {

public static ArrayList<Integer> lengths(ArrayList<String> strings) {
        ArrayList<Integer> list = new ArrayList<Integer>();

        for (String item : strings) {
            System.out.print(item.length());
        }
        return list;
    }

public static void main(String[] args) {

ArrayList<String> list = new ArrayList<String>();

        list.add("Dog");
        list.add("Elephant");
        list.add("Aligator");
        list.add("Parrot");

        ArrayList<Integer> stringLenghts = lengths(list);
        System.out.println(stringLenghts);

    }
}

And this program outputs

3386[]

Instead of

[3, 3, 8, 6]

Any idea where I do a mistake?

Upvotes: 3

Views: 2606

Answers (4)

vijayraj34
vijayraj34

Reputation: 2415

Initially you are printing the length of each string from the loop

for(String item : strings) {
     System.out.print(item.length());
}

which outputs : 3386 (no new line because of print)

After this empty ArrayList is printed because you are returning the empty arrayList object.

System.out.println(stringLenghts);

which outputs : [ ]

Solution :
You have to replace
System.out.print(item.length());
with
list.add(item.length());

Upvotes: 2

Aritra Chatterjee
Aritra Chatterjee

Reputation: 460

Try this.

import java.util.ArrayList;

public class Test {

public static ArrayList<Integer> lengths(ArrayList<String> strings) {
    ArrayList<Integer> list = new ArrayList<Integer>();

    for (String item : strings) {
        list.add(item.length());
    }
    return list;
}

public static void main(String[] args) {

ArrayList<String> list = new ArrayList<String>();

    list.add("Dog");
    list.add("Elephant");
    list.add("Aligator");
    list.add("Parrot");

    ArrayList<Integer> stringLenghts = lengths(list);
    System.out.println(stringLenghts);

}
}

EDIT: In the lengths method, the list is being created but you are not adding any items to it. Now in the code abode for (String item : strings) { list.add(item.length()); } This adds the length of each string to the new ArrayList which you will return to the main method for printing.

Thanks

Upvotes: 3

Vasyl Lyashkevych
Vasyl Lyashkevych

Reputation: 2232

You need correct this body of cycle and add here the numbers

list.add(item.length());

between brackets.

Upvotes: 2

Vasu
Vasu

Reputation: 22462

In your lengths() method, you are simply printing the length values. But you are NOT adding the lengths to the ArrayList i.e., your stringLenghts list is empty (so printing as [] empty array), so change your code as shown below:

public static ArrayList<Integer> lengths(ArrayList<String> strings) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (String item : strings) {
            list.add(item.length());//add length to list
        }
        return list;
}

Upvotes: 3

Related Questions