1

In my application I am storing string type data in arraylist with "|" operator as separator. i.e my array-list contains data like [|123,124,|,324,543|789,649,666,356] Now I need to split the arraylist with "|" and need to store the data into a string array. How can I do that?

My Code:

ArrayList<String> arr2;
ArrayList<ArrayList<String>> alllist;
String[] str;

for (j = 0; j < alllist.size(); j++) {
    arr2 = new ArrayList<String>();
    arr2 = alllist.get(j);

    for (Object v : arr2) {
        str = v.toString().split("|");
        System.out.println("checking str[i]" + str[i]);

        if (str[j].contains(",")) {
            System.out.println("hii");
        } 
    }
}

It is showing error at if(str[j].contains(",")) line....Please help me regarding this.. thanks in advance...

3
  • 1
    What type does the str variable have? AFAIK, the split() method returns an array of Strings, but the String class does not have the contains() method. Commented Mar 19, 2012 at 13:53
  • the error is null pointer exception Commented Mar 19, 2012 at 13:54
  • Can you post code that is compilable, puts some sample data in your ArrayList, and let us know what output you expect so that we better understand what you are trying to achieve? Commented Mar 19, 2012 at 14:04

3 Answers 3

1

Not sure what you want to achieve, but this could help you to build your code:

public static void main(String args[]) throws ParseException {

    List<List<String>> allList = new ArrayList<List<String>>();
    allList.add(Arrays.asList("|123,124,|,324,543|789,649,666,356"));

    for (List<String> list : allList) { //use a for each loop instead of looping on an int and using allList.get(i)
        for (String s : list) { //Same thing
            String[] str = s.split("\\|"); //You need to escape | with \\ to get the expected split
            for (String item : str) {
                System.out.println("checking " + item);
                if (item.contains(",")) {
                    System.out.println("hii");
                }
            }

        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Why Don't you simply use this :

List<String> flag2=new ArrayList<String>();
String flag[]=new String[flag2.size()];

for(int i=0;i<flag2.size();i++){
    flag[i]=flag2.get(i);
}

Comments

0
public void onClick(View v){
    String teks = txtinput.getText().toString();
    String[] tempPesanMasuk = teks.split("_");
    ArrayList<String> teks_lengkap = new ArrayList<String>();
    String teksJadi = "";

    for(int i = 0; i < tempPesanMasuk.length; i++)
        teks_lengkap.add(tempPesanMasuk[i].trim());

    for(int i = 0; i < tempPesanMasuk.length ; i++)
        teksJadi = teksJadi + teks_lengkap.get(i) + " ";

    System.out.println(teksJadi);
}

1 Comment

Welcome to Stack Overflow! Please explain what your code does and why it will solve the problem. An answer that just contains code (even if it's working) usually wont help the OP to understand their problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.