質問

Is there a way to implement this in a ternary operation. I'm very new to that ternary stuff, maybe you could guide me.

if(selection.toLowerCase().equals("produkt"))
     cmdCse.setVisible(true);
else
     cmdCse.setVisible(false);

This one doesn't seem to work.

selection.toLowerCase().equals("produkt")?cmdCse.setVisible(true):cmdCse.setVisible(false);
役に立ちましたか?

解決

In this case, you don't even need a ternary operator:

 cmdCse.setVisible(selection.toLowerCase().equals("produkt"));

Or, cleaner:

 cmdCse.setVisible(selection.equalsIgnoreCase("produkt"));

Your version:

selection.toLowerCase().equals("produkt")? cmdCse.setVisible(true): cmdCse.setVisible(false);

is semantically incorrect: ternary operator should represent alternative assignments, it's not a full replacement for if statements. This is ok:

double wow = x > y? Math.sqrt(y): x;

because you are assigning either x or Math.sqrt(y) to wow, depending on a condition.

My 2cents: use ternary operator only when it makes your program clearer, otherwise you will end up having some undecipherable one-liners.

他のヒント

Perhaps

cmdCse.setVisible(selection.toLowerCase().equals("produkt"));

The ternary operator isn't exactly like an if statement. A ternary operator has to "return" something from both sides, so putting void method calls like setVisible() there won't work.

Instead, you could do something like this without ternary operators at all:

cmdCse.setVisible(selection.toLowerCase().equals("product"));

But just to demonstrate the point, the ternary equivalent would look something like this:

cmdCse.setVisible(selection.toLowerCase().equals("product") ? true : false);

Notice how now the ternary operator "returns" true or false on both sides instead of simply calling a void method.

I think this will work for you

cmdCse.setVisible(selection.toLowerCase().equals("produkt"));

Directly from the docs

Use the ?: operator instead of an if-then-else statement if it makes your code more readable; for example, when the expressions are compact and without side-effects (such as assignments).

In your case cmdCse.setVisible(true / false); doesn't return anything, and the operation also has side effects (it changes state of cmdCse), so the conditional operator cannot be used here (when you do use the operator, both of the ? and : branches must have the same return type).

As an aside, please note that .. ? .. : .. should be referred to as the conditional operator

On the issue of using exceptions

I want to answer the issue of the exceptions here as this question is a duplicate for another question concerning throwing exceptions from a ternary expression, but this is not addressed in the above answers.

The general consensus is that it cannot be done directly as in:

public Character next() {
  return hasNext() ? s.charAt(cur++) : throw new NoSuchElementException(); // compilation error
}

will give you a compilation error, but as Clement pointed out it can be done via a declared extra function. It can also be done via (ab-)using lambda expressions.

public Character next() {
  return hasNext() ? s.charAt(cur++) : ((Function<Integer, Character>) x -> {throw new NoSuchElementException();}).apply(1);
}

for sure this is not that pretty (it is pretty ugly) and I would not recommend to do that for readability purposes, but sometimes there are circumstances which might warrant exceptionally doing that. If someones figures out a way to do it without the cast it would be a bit more readable.

If you had a function throwNoSuchElementException() defined somewhere that you use more than once, it would look a bit more readable:

public Character next() {
  return hasNext() ? s.charAt(cur++) : throwNoSuchElementException();
}

(P.S.: I included this answer for completeness sake, as I asked myself can it really be not done?)

(P.S.S.: If the exception to be thrown is not a runtime exception this will not work so easily and would require even more handstands - not really worth it)

Here are my tips, if you need to set things to booleans, then simple use setBoolean(condition), else, if you need to set a variable to a non boolean value, then use var=condition?result1:result2(or the variable itself if you don't want to change if condition is false), otherwise, use if else.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top