1

I have the below method call. If name or desc is null, it uses the word null. Instead I would like an empty string.

How to achieve this as part of the method call itself. I don't want to do this outside of the method call with if conditions.

boolean creatok = users.create (String.valueOf(name), String.valueOf(desc));
2
  • 3
    Either use a library that supplies a default value, or write one yourself and use it instead. Commented Jun 25, 2015 at 22:23
  • I believe the only option without conditional checking is to use a ternary operator. Commented Jun 25, 2015 at 22:25

3 Answers 3

4
boolean creatok = users.create (Objects.toString(name, ""), Objects.toString(desc, ""));
Sign up to request clarification or add additional context in comments.

Comments

1

I think you should use conditions, if you want to avoid 'if' clauses, use a ternary operator

boolean creatok = users.create(name == null? "" : String.valueOf(name), name == null? "" : String.valueOf(name));

2 Comments

boolean createok = users.create (String.valueOf(name) == null ? " " : String.valueOf(name), String.valueOf(desc) == null ? " " : String.valueOf(desc)) It still returned null. What am I doing wrong
null as String or null as reference ?
0

You can do something like this if you don't want to write if else

boolean creatok = users.create (name == null ? "" : name, desc == null ? "" : desc);

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.