Based on the following:
int x = ((int)(bound * Math.random()));
int z = ((int)(bound * Math.random()));
String b =
JOptionPane.showInputDialog("What is " + x + " times " + z + " ?");
int guess = new Integer(b).intValue();
boolean test2 = (guess == (x * z));
String q = "correct";
String p = "incorrect";
I would like to make the following constant, k:
String k = if (test2 == true)
System.out.print(q);
else
System.out.print(p);
But is that not possible? I get the following error:
Error: illegal start of expression
js
Boudewijn Dijkstra - 24 Nov 2004 21:19 GMT
> Based on the following:
>
[quoted text clipped - 20 lines]
>
> Error: illegal start of expression
That is because an if-block doesn't return a value. Also, the
System.out.print method prints characters to the console (the standard
output), not to a string. I suggest the following statement:
String k = test2 ? p : q;
Eric Sosman - 24 Nov 2004 21:26 GMT
> Based on the following:
>
[quoted text clipped - 16 lines]
> else
> System.out.print(p);
I think what you want is
String k = (test2 == true) ? q : p;
System.out.print(k);
or more simply
String k = test2 ? q : p;
System.out.print(k);
or more simply still
System.out.print(test2 ? q : p);

Signature
Eric.Sosman@sun.com
Oscar kind - 24 Nov 2004 21:33 GMT
> I would like to make the following constant, k:
>
> String k = if (test2 == true)
> System.out.print(q);
> else
> System.out.print(p);
This cannot be done using an if-statement; you'll ahve to use an
expression. Luckily, there is one: the ternary operator ?: :
String k = (test2 == true) ? q : p;
or simply:
String k = test2 : q : p;
Note however, that this can easily become unreadable. So unless it is for
something trivial like the example above, I'd not use it.

Signature
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website
PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
Hal Rosser - 25 Nov 2004 02:41 GMT
> I would like to make the following constant, k:
>
> String k = if (test2 == true)
> System.out.print(q);
> else
> System.out.print(p);
String k = p;
if (test2 ){ k = q; }
(But then, its not a constant)
If I recall correctly, Constants are assigned their value at compile time,
but variables are assigned their values at run time - so you can't assign a
variable to a constant.
this would not work
public const String K = p;
JS - 25 Nov 2004 11:42 GMT
Thank you all for introducing me with the ternary operator ?: . It now works
but I am still a bit confused about what ? means and what : means.
Js
> Based on the following:
>
[quoted text clipped - 22 lines]
>
> js
Chris Smith - 25 Nov 2004 14:39 GMT
> Thank you all for introducing me with the ternary operator ?: . It now works
> but I am still a bit confused about what ? means and what : means.
Those two characters are not separate operators; they are part of one
expression. You MUST write something like:
<condition> ? <exp1> : <exp2>
And then here's what happens:
1. <condition> is evaluated as a boolean expression
2. If <condition> is true, then <exp1> becomes the result
3. If <condition> is false, then <exp2> becomes the result

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
V S Rawat वी एस रावत - 25 Nov 2004 17:42 GMT
> Thank you all for introducing me with the ternary
> operator ?: . It now works but I am still a bit confused
> about what ? means and what : means.
>
> Js
That is the syntax.
k = test2 ? p : q;
test2 is evaluated first.
if test2 evaluates to true, k will be assigned the first
value p.
if test2 evaluates to false, k will be assigned the second
value q.
it is equivalent to the following:
if (test2) {
k = p;
}
else {
k = q;
}
it is just a shorthand. it will get very confusing if test2,
p or q are lengthy statements. hence, avoid using = ? :
operator, and use the above compelete good old "if then
else" until you are comfortable.

Signature
Rawat
Eric Sosman - 29 Nov 2004 22:38 GMT
> Thank you all for introducing me with the ternary operator ?: . It now works
> but I am still a bit confused about what ? means and what : means.
`?' means `then' and `:' means `else', more or less.
That's actually the way the ternary operator was spelled
in Algol, where you could write (IIRC)
x := if a then b else c;
In Java, this is written as
x = a ? b : c;

Signature
Eric.Sosman@sun.com