Menu
Topics Index
...
`

Group Strings By Length


If you need explanation Read this topic

If you need Answer Take test on this topic

Write a program to group the string objects by length. Strings less than 10 characters should be added to the first group, Strings with exactly 10 characters should be added to the second group and Strings greater than 10 characters should be added to the third group.

Input (ArrayList containing Strings) Outputs - Less Than Group, Equals Group, Greater Than Group (All of them are ArrayLists containing Strings)
["Andhra Pradesh", "Tamil Nadu", "Maharashtra", "Madhya Pradesh", "Gujarat", "Karnataka"] Less than group = ["Gujarat", "Karnataka"]
Equals group = ["Tamil Nadu"]
Greater than group = ["Andhra Pradesh", "Maharashtra", "Madhya Pradesh"]
["Sachin Tendulkar", "Yuvraj", "Harbhajan", "Virat Kohli", "Virendar Shewag", "MS Dhoni", "Suresh Raina", "Rohit"] Less than group = ["Yuvraj", "Harbhajan", "MS Dhoni", "Rohit"]
Equals group = [EMPTY]
Greater than group = ["Sachin Tendulkar", "Virat Kohli", "Virendar Shewag", "Suresh Raina"]


import java.util.*;
class GroupStringsByLength
{
    public static void main(String s[])
    {
        ArrayList<String> input = new ArrayList<String>();
        input.add("Andhra Pradesh");
        input.add("Tamil Nadu");
        input.add("Maharashtra");
        input.add("Madhya Pradesh");
        input.add("Gujarat");
        input.add("Karnataka");
        
        ArrayList<String> lessThanGroup = new ArrayList<String>();
        ArrayList<String> equalsGroup = new ArrayList<String>();
        ArrayList<String> greaterThanGroup = new ArrayList<String>();
        
        groupStringsByLength(input, lessThanGroup, equalsGroup, greaterThanGroup);
        
        System.out.println("Less Than Group = " + lessThanGroup);
        System.out.println("Equals Group = " + equalsGroup);
        System.out.println("Greater Than Group = " + greaterThanGroup);

    }


    public static void groupStringsByLength(List<String> input,
                    List<String> lessThanGroup,
                    List<String> equalsGroup,
                    List<String> greaterThanGroup)
    {
    }

}
Topic: Java ArrayList

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

*take for loop and initialize loop control to 0 and condition <input.size();
*
now check the length of the element by getting it using get(i)...
if  input.get(i).length()<9 than add to lessThanGroup.add(input.get(i))
else
if(input.get(i).length()==10)
equalsGroup.add(input.get(i))
else
greaterThanGroup.add(input.get(i))

Posted by Uday Kumar    2014-12-19 08:05:44


for grouping purpose... firstly we need to traverse every element of the list... for traversing we need a for loop.. wich will traverse frm startng til end ...i=1 to list. length.. now inside this loop... chek. length of every string of list... if string.length<10 then add this element to less than group using lessthan group.add(input.get(i))....else if length=10 then add to equalsgrp else add to greatr dan grp... exit loop

Posted by Asma Mujtaba Khan    2014-12-19 12:34:16


1.we will create an object of iterator which is used to traverse elements in arraylist.2.while loop is used till the hasnext() element returns false I.e. till the end of elements.3.in while loop we find size.of element with the help of hasNext().length() method.4.we will add the elememts as per the length of element in arraylist using if else....CODE iterator it=input.iterator();while(it.hasNext()){size=it.next().length();if(size<10)lessthanGroup.add(it.next());else if(size==10)equalsgroup.add(it.next());else greatergroup.add(it.next);}

Posted by Mânïshå Mùlchåndânï    2014-12-20 10:43:57


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Asma Mujtaba Khan, Mânïshå Mùlchåndânï,  for 'Second Correct Comment'. The 'lucky liker' is Asma Mujtaba Khan. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-12-23 05:07:39


© meritcampus 2019

All Rights Reserved.

Open In App