Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2004

Tip: Looking for answers? Try searching our database.

How do I define this constant?

Thread view: 
JS - 24 Nov 2004 20:33 GMT
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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.