0

I am using this code and ds list prints for example:

aaa.(bbb)
aaa.(eee)
ccc.(ddd) 
...

I need it to print the strings related to aaa in the same brackets using, to separate them.

Example: aaa.(bbb,eee)

What should I change in my code?

I know the code is not complete, but it would complicate it a lot if I added everything. The goal is while iterating on templist for the string s, to add templist elements in the format mentioned.

List<String> templist = new ArrayList<String>() ;
List<String> ds = new ArrayList<String>() ;

String s = "aaa"

String selecfin = null ;

for(int j =0;j<templist.size(); j++){

       String selecP = templist.get(j);

       selecfin = s+".("+selecP+")";
       ds.add(selecfin);
}
2
  • According to your code, it can't print anything but aaa.(xxx). When does the value in s change? Commented Dec 4, 2013 at 17:06
  • I suggest that you create a structure like Map<String, List<String>>, where you put the root ("aaa", "ccc") as the Keys and add to a List of strings for the Values. Commented Dec 4, 2013 at 17:08

2 Answers 2

2

I didn't test it, but you can try it like this

List<String> templist = new ArrayList<String>() ;
List<String> ds = new ArrayList<String>() ;

String s = "aaa";

String selecfin = null ;
String tmp = null;

for(int i=0; i<templist.size(); i++) {
  if(tmp != null) {
    tmp = tmp + "," + templist.get(i);
  } else {
    tmp = templist.get(i);
  }
}

selecfin = s + ".(" + tmp + ")";

ds.add(selecfin);
Sign up to request clarification or add additional context in comments.

1 Comment

Please do not post what has not been tested. At least run it on IdeOne
0

You can check the presence of aaa as follows:

if(selecP.contains("aaa")){
  // separate and do what you need
}  

to add templist elements in the format mentioned. I do not quite understand what that means.

Also, you can make the code a little more compact by using the for-each loop as follows:

for(String selecP : tempList){
  if(selecP.contains("aaa"){
      // something
  }
}  

You aren't adding anything to your lists in the code snippet you provided. You are iterating over empty lists.


SSCCE:
Run it here: http://ideone.com/66EIax

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        List<String> tempList = new ArrayList<String>();
        List<String> ds = new ArrayList<String>();

        tempList.add("aaa.(bbb)");
        tempList.add("aaa.(eee)");
        tempList.add("ccc.(ddd)");

        String s = "aaa";

        for(String selecP : tempList){
            if(selecP.contains(s)){
                ds.add(new String(selecP));
            }
        }

        for(String each : ds){
            System.out.println(each);
        }
    }
}  

Output:

aaa.(bbb)
aaa.(eee)

1 Comment

This output is not what the OP wants; the required output in this case is aaa.(bbb,eee).

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.