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 / General / December 2005

Tip: Looking for answers? Try searching our database.

<= cannot be applied to boolean, int ?!?

Thread view: 
machoextreme - 04 Dec 2005 07:48 GMT
I was experimenting with an homework assignment(it called for
if-then-else statements, which I finished thanks to some help from this
forum), and was trying to see if I could accomplish it using boolean
operators. However I get the following error operator <= cannot be
applied to boolean, int. Any idea why?!?
import java.util.*;

public class activities2
{
    public static void main(String[] args)
{
    int temp;
    String act=null;
    boolean swim = (temp > 85);
    boolean tennis = ( 70 < temp <= 85);
    boolean golf = (32 < temp <= 70);
    boolean skiing = (0 < temp <= 32);
    boolean dancing = (temp <= 0);
    Scanner keyboard= new Scanner(System.in);
    System.out.println("In order to decide what is the best activity at
the present time, please enter the current temperature ");
    temp = keyboard.nextInt();
    if (swim)
        act = "Swimming";
    else if (tennis)
        act = "Tennis";
    else if (golf)
        act = "Golf";
    else if (skiing)
        act = "Skiing";
    else if (dancing)
        act = "Dancing";
    System.out.println("The recommened activity for the current
temperature is " + act);
   
   
   
   
}
}
ddk - 04 Dec 2005 08:04 GMT
For example:  70 < temp <= 85
The result of " 70 < temp " is boolean, i.e.  true or false.
What's the result of " boolean <= 85 " ?
It's obvious that the expression is wrong.
machoextreme - 04 Dec 2005 08:27 GMT
So your saying the reason why the expression doesn't work is that I'm
trying to evaluate a result, and not a varaible. So if I change the
boolean to boolean tennis =(70< temp) || (temp<= 85) would that be more
accurate?
ddk - 04 Dec 2005 08:37 GMT
you are right. but the statement should be :
boolean tennis =(70< temp) && (temp<= 85)
Roedy Green - 04 Dec 2005 09:19 GMT
>For example:  70 < temp <= 85
>The result of " 70 < temp " is boolean, i.e.  true or false.
>What's the result of " boolean <= 85 " ?
>It's obvious that the expression is wrong.

In math you can say this, and in COBOL, but Java insists you spell out
such expressions longhand.

For some examples see http://mindprod.com/jgloss/boolean.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Adam Maass - 04 Dec 2005 20:40 GMT
>I was experimenting with an homework assignment(it called for
> if-then-else statements, which I finished thanks to some help from this
> forum), and was trying to see if I could accomplish it using boolean
> operators. However I get the following error operator <= cannot be
> applied to boolean, int. Any idea why?!?
> import java.util.*;

< cannot be applied to boolean.

< on int has a boolean return type. Boolean cannot be assigned to an int
variable.

-- Adam Maass
Thomas G. Marshall - 05 Dec 2005 04:14 GMT
Adam Maass said something like:
>> I was experimenting with an homework assignment(it called for
>> if-then-else statements, which I finished thanks to some help from this
[quoted text clipped - 7 lines]
> < on int has a boolean return type. Boolean cannot be assigned to an int
> variable.

I believe that he understands /that/ as such.  What he was doing was
attempting a compound comparison similar to

   (a < b < c)

Where the first a<b becomes the boolean.

Signature

Puzzle: You are given a deck of cards all face down
except for 10 cards mixed in which are face up.
If you are in a pitch black room, how do you divide
the deck into two piles (may be uneven) that each
contain the same number of face-up cards?
Answer (rot13): Sebz naljurer va gur qrpx, qrny bhg
gra pneqf naq syvc gurz bire.



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.