Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split string and store in arraylist

I want to split the below mentioned string and save it in two seperate arraylist like state and city

 public class RoundValue {

    public static void main(String args[]) {
        String firstset = null;

        String city = "Tamilnadu;chennai-madurai-salem::Kerala;cochin-tiruvandrum-calicut";

        ArrayList<String> mState = new ArrayList<String>();
        ArrayList<String> mCity = new ArrayList<String>();

        HashMap<String, List<String>> hashsplit = new HashMap<String, List<String>>();

        List<String> splitword1 = Arrays.asList(city.split("::"));

        if (splitword1.size() > 0) {
            for (int i = 0; i < splitword1.size(); i++) {
                firstset = splitword1.get(i);


                List<String> firststate = Arrays.asList(firstset.split("-"));
                if (firststate.size() > 0) {
                    for (int j = 0; j < firststate.size(); j++) {
                        String firstcity = firststate.get(j);

                        List<String> secondcity = Arrays.asList(firstcity.split(";"));
                        if (secondcity.size() > 0) {
                            for (int k = 0; k < secondcity.size(); k++) {
                                String septcity = secondcity.get(k);
                                System.out.println("septcity Splitted:" + septcity);

                            }
                        }
                    }
                }
            }
        }

    }
}

I have splitted each and every character, but i have to store state in seperate list and city in seperate list

like image 876
user3691107 Avatar asked Jun 17 '14 05:06

user3691107


1 Answers

Step 1: Split your given string input as Arrays.asList(city.split("::"));, you alresy did it.

Step 2: Split each list in to array like, example Tamilnadu;chennai-madurai-salem using String both[]=string.split(";"); here you will get seperated state and cities. like both[0] is State. both[1] is chennai-madurai-salem

Step 3: Split the cities string in both[1] usig both[1].split("-")

So Demo code I have given as below. You can modify.

public static void main(String[] args) {
        String city = "Tamilnadu;chennai-madurai-salem::Kerala;cochin-tiruvandrum-calicut";
        ArrayList<String> mState = new ArrayList<String>();
        ArrayList<String> mCity = new ArrayList<String>();
        List<String> bothList= Arrays.asList(city.split("::"));

        for (String string : bothList) {
            String both[]=string.split(";");
            String state=both[0];
            List<String> tempCityList=Arrays.asList(both[1].split("-"));
            mState.add(state);
            mCity.addAll(tempCityList);
        }
        System.out.println("Your states");
        for (String string : mState) {
            System.out.print(string+" ");
        }
        System.out.println("\nYour cities");
        for (String string : mCity) {
            System.out.print(string+" ");
        }


    }
like image 86
mahesh Avatar answered Oct 05 '22 08:10

mahesh