Desc: compareChar returns true or false. if true it sets the value of button, if false do nothing.
I am trying to use:
if compareChar(curChar, toChar("0")) ? getButtons().get(i).setText("§");
netbeans is saying:
')' excepted
':' excepted
I tried these combinations:
if compareChar(curChar, toChar("0")) ? getButtons().get(i).setText("§");
if compareChar(curChar, toChar("0")) ? getButtons().get(i).setText("§") : ;
if compareChar(curChar, toChar("0")) ? getButtons().get(i).setText("§") :
if (compareChar(curChar, toChar("0"))) ? getButtons().get(i).setText("§");
if (compareChar(curChar, toChar("0"))) ? getButtons().get(i).setText("§") : ;
if (compareChar(curChar, toChar("0"))) ? getButtons().get(i).setText("§") :
This question is related to
java
if-statement
inline
cond? statementA: statementB
Equals to:
if (cond)
statementA
else
statementB
For your case, you may just delete all "if". If you totally use if-else instead of ?:. Don't mix them together.
(inline if) in java won't work if you are using 'if' statement .. the right syntax is in the following example:
int y = (c == 19) ? 7 : 11 ;
or
String y = (s > 120) ? "Slow Down" : "Safe";
System.out.println(y);
as You can see the type of the variable Y is the same as the return value ...
in your case it is better to use the normal if statement not inline if as it is in the pervious answer without "?"
if (compareChar(curChar, toChar("0"))) getButtons().get(i).setText("§");
Syntax is Shown below:
"your condition"? "step if true":"step if condition fails"
This should be (condition)? True statement : False statement
Leave out the "if"
Your cases does not have a return value.
getButtons().get(i).setText("§");
In-line-if is Ternary operation all ternary operations must have return value. That variable is likely void and does not return anything and it is not returning to a variable. Example:
int i = 40;
String value = (i < 20) ? "it is too low" : "that is larger than 20";
for your case you just need an if statement.
if (compareChar(curChar, toChar("0"))) { getButtons().get(i).setText("§"); }
Also side note you should use curly braces it makes the code more readable and declares scope.
Source: Stackoverflow.com