I am trying to debug this error.
unexpected syntax: 'missing SEMICOLON at 'if''
public Boolean validateName(String name){
return if ( name == null || name =='' || name.length() < 3) ? true : false;
}
Any idea what I am missing?
I am trying to debug this error.
unexpected syntax: 'missing SEMICOLON at 'if''
public Boolean validateName(String name){
return if ( name == null || name =='' || name.length() < 3) ? true : false;
}
Any idea what I am missing?
Best Answer
Ternary operators don't need
if
defined. So your return statement should just read:Or, as @AdrianLarson pointed out in the comments you could just use:
Which saves a few bytes of code whilst still delivering the same result.
Edit
There's a few ways to achieve what you want here, based off of the comments on this post. For instance, from IllusiveBrian:
This returns a boolean value depending on whether or not the String is empty. Pretty self explanitory!
Additionally, Sebastian Kessel mentions you can use:
Would also return a boolean value whether or not the String is empty.
The difference between these functions are as follows:
These are from the docs.
The long story short is that there's a number of ways to handle your specific use case. You can use many functions that will return either true or false.
But, with respect to ternary operators, you just didn't need the
if
part meaning if you, for instance, wanted a ternary operator that returned a String, you'd do so as follows: