user4544501
user4544501

Reputation:

Filter array elements by length

So i want to be able to ask the user to enter the length of an array and fill it with names. After doing so, the program will print out the names with the length of the string next to them.

public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    String names [];

    // Read the number of kids
    System.out.print("How many kids are there? ");
    int size = sc.nextInt();

    // Read the names of the kids
    String [] kids = new String [size];

    for (int k = 0; k < kids.length; k++)
    {
        System.out.print("Enter name of kid #" + k + ": ");
        kids[k] = sc.next();
    }
    // Print all the names of the kids and the length of their names
    System.out.println("Names of kids with lengths are: ");

    for (int i = 0; i < kids.length; i++)
    {
      System.out.println(kids[i] + " " + kids[i].length());
    }

after this i want the user to enter an integer and the program will search the array for any strings that are that length, if it finds any i want it to print out the initials of all the ones it finds. I have to use to methods for this program. But i can't seem to wrap my head around this im stuck.

static int countByLength(String [] names, int length)
{
    int count = 0;



    return count;
}


/**
 * Filter an array of names and keep only those names with a given length
 * @param names: an array of names
 * @param length: an integer length
 * @return the array of names that have the given length
 */
static String [] filterByLength(String [] names, int length)
{
    String [] filtered = new String[length];

    for(int k = 0; k < filtered.length; k++)
    {
        filtered [k] = 
    }

    return filtered; 
}

}

Upvotes: 1

Views: 2732

Answers (4)

Anderson Vieira
Anderson Vieira

Reputation: 9049

If you are using Java 8 and use a List instead of an array you can use filter to achieve this in one line of code:

List<String> filterByLength(List<String> names, int length) {
   return names.stream().filter(n -> n.length() == length).collect(Collectors.toList());
}
  • names.stream() turns the List into a Stream
  • filter(n -> n.length() == length) keeps only the elements in the stream whose length is equal to length.
  • collect(Collectors.toList()) returns a List from the Stream

Upvotes: 1

Leander
Leander

Reputation: 1395

One problem is, that you don't know how many results you will have, so an array is not the best datastructure. I would recommend using a list instead, for example ArrayList. Then you can just iterate over the array, adding all the matching Strings to the list.

static List<String> filterByLength(String [] names, int length)
{
   List<String> filtered = new ArrayList<String>();

   for(int k = 0; k < filtered.length; k++)
   {
       if (names.length() == length) filtered.add(names[k]);
   }

   return filtered; 
}

Upvotes: 3

Randyka Yudhistira
Randyka Yudhistira

Reputation: 3652

I will answer the question with the method that you've created, ignoring its effective or ineffective solution.

// Count the array that have name with same length
static int countByLength(String [] names, int length)
{
    int count = 0;

    for(int i = 0;i < names.length;i++)
    {
      if(names[i].length() = length) count++;
    }


    return count;
}


/**
 * Filter an array of names and keep only those names with a given length
 * @param names: an array of names
 * @param length: an integer length
 * @return the array of names that have the given length
 */
static String [] filterByLength(String [] names, int length)
{
    String [] filtered = new String[countByLength(names, length)];
    int index = 0;

    for(int k = 0; k < filtered.length; k++)
    {
        if(names[k].length() == length) filtered[index++]=names[k];
    }

    return filtered; 
}

Upvotes: 1

aib
aib

Reputation: 46951

Bad choice of variable (parameter) names has added to your confusion. The length parameter in countByLength is the length of names to look for, and it looks like so is the length in filterByLength. Yet you are using length in filterByLength as the length of the names array -- the total number of names, in other words.

If you really want to use a plain array (and I would suggest heeding @Jack's suggestion in the comment or, edit: @Leander's answer) it would probably be easiest to count the number of names that match first, then create and populate a new names array with that many elements. It looks like you want to do the counting by simply getting the size of the filtered array, and that's why this has become a chicken-and-egg problem.

Upvotes: 0

Related Questions