1

In a Java code, I have 11 different strings, some of them (at most 9) can be null. Strings are a part of an object. If none of them is null, I make a string like this :

string1 + "," + string2 + "," + string3 + "," + string4 + "," + string5 + "," + string6 + "(" + string7 + ")" + string8 + string9 + "+" + string10 + string11

If none of them is null, it's okey. But some of them can be null. If I check if each of them is null, code gets really slow and long. How can I generate such a string in an efficient and fast way? Thanks.

7
  • 2
    Can you use an array? Commented Oct 27, 2015 at 12:28
  • What result do you want to get when the strings are null? As it stands, you'll just get the string null in your result - there won't be an error or anything like that. Commented Oct 27, 2015 at 12:30
  • @Arc676 Could you show with a simple example? Thanks. Commented Oct 27, 2015 at 12:33
  • 2
    You can declare a helper method String emptyForNull(String s) { return s == null ? "" : s; } Commented Oct 27, 2015 at 12:34
  • 1
    The above comment can help with that. Also, jason, I need to know how you get your strings first. You should add that to your question Commented Oct 27, 2015 at 12:35

5 Answers 5

3
public static void main(String[] args) {
    String string1 = "test1";
    String string2 = "test2";
    String string3 = "test3";
    String string4 = null;
    String string5 = "test5";

    StringBuilder stringBuilder = new StringBuilder();

    List<String> valueList = new ArrayList<>();
    valueList.add(string1);
    valueList.add(string2);
    valueList.add(string3);
    valueList.add(string4);
    valueList.add(string5);
    // etc.

    for (String value : valueList) {
        if (value != null) {
            stringBuilder.append(value);
        }
        else {
            value = ",";
            stringBuilder.append(value);
        }
    }
    System.out.println(stringBuilder);
}

Output :

test1test2test3,test5

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

1 Comment

The problem is that there are parts in the output string that are supposed to be in parentheses, parts that are not separated by comma, parts that are separated by +...
2

With Java 8 you could use this for the first part with comma-delimited strings:

String part1 = Stream.of(string1, string2, string3, string4, string5, string6)
                     .filter(Objects.notNull())
                     .collect(joining(",");

From then on you have an irregular structure so you'll need custom logic to handle it. The helper function

static String emptyForNull(String s) {
  return s == null ? "" : s;
}

can get you part of the way.

Comments

1

In Java 8, you can stream and join with Collectors, and filter with Objects classes

List<String> strings = Arrays.asList("first", "second", null, "third", null, null, "fourth");
String res = strings.stream().filter(Objects::nonNull).collect(Collectors.joining(","));
System.out.println(res);

results in output

first,second,third,fourth

Comments

1

If you simply want your code to be as short as possible, use String.format and get rid of the "null" in the string.

String result = String.format("%s,%s,%s,%s,%s,%s(%s)%s,%s,%s,%s",
            string1, string2, string3, string4, string5,
            string6, string7, string8, string9, string10,
            string11);

    System.out.println(result.replace("null", ""));

Comments

1

If 6th index of your string is always surrounded by ( and ) then you can use following code.

    List<String> vals = new ArrayList<>();
    vals.add(string1);
    vals.add(string2);
    vals.add(string3);
    vals.add(string4);
    vals.add(string5);
    vals.add(string6);
    vals.add(string7);
    vals.add(string8);
    vals.add(string9);
    vals.add(string10);
    vals.add(string11);

    for (int i =0 ; i < vals.size(); i++) {
        // check null values
        if (vals.get(i) != null) {
        // as your requirement surround 7th value with ( and )
            if(vals.indexOf(vals.get(i)) == 6) {
                values += "\b(" + vals.get(i)+")";
            } else {
                values += vals.get(i)+",";
            }
        }
    }
    System.out.println(values+"\b");

Output if 4th and 9th strings are null then,

test1,test2,test3,test5,test6(test7)test8,test10,test11

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.