0

In my project I need to store the values dynamically in a string and need to split that string with ",". How can I do that ? Please help me..

My Code:

static ArrayList<ArrayList<String>> listhere;
ArrayList<String> arropids;
String arropids1; 


    for(int q=0;q<listhere.size();q++)
                {
                  arropids = listhere.get(q);

                  if(arropids.get(3).equals("1"))
                  {
                      arropids1 += arropids.get(0) + ","; 


                  System.out.println("arropids1"+arropids1);

                }
                } 
2
  • So you want to store each arropids1 that gets parsed from the data? Commented Apr 14, 2012 at 4:57
  • yes...after storing I want to split each arropids1 ... Commented Apr 14, 2012 at 5:03

2 Answers 2

2

You must be getting NullPointerException as you havent initialized the String, initialize it as

String arropids1="";

It will resolve your issue, but I dont Recommend String for this task, as String is Immutable type, you can use StringBuffer for this purpose, so I recommend following code:

static ArrayList<ArrayList<String>> listhere;
ArrayList<String> arropids;

StringBuffer buffer=new StringBuffer();

    for(int q=0;q<listhere.size();q++)
                {
                  arropids = listhere.get(q);

                  if(arropids.get(3).equals("1"))
                  {
                      buffer.append(arropids.get(0));
                      buffer.append(","); 


                  System.out.println("arropids1"+arropids1);

                }
                }

and finally get String from that buffer by:

 String arropids1=buffer.toString(); 
Sign up to request clarification or add additional context in comments.

Comments

0

In order to split the results after storing your parse in the for loop, you use the split method on your stored string and set that equal to a string array like this:

static ArrayList<ArrayList<String>> listhere;
ArrayList<String> arropids;
String arropids1 = ""; 


for(int q=0;q<listhere.size();q++) {
              arropids = listhere.get(q);

              if(arropids.get(3).equals("1"))
              {
                  arropids1 += arropids.get(0) + ","; 


              System.out.println("arropids1"+arropids1);

              }
      }
      String[] results = arropids1.split(",");
      for (int i =0; i < results.length; i++) {
           System.out.println(results[i]);
      }

I hope that this is what you're looking for.

Comments

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.