![]() |
[java] dumb question
so, i'm trying to initialize a variable to only be able to be assigned, 0, 90, 180, 270, or 360. the line of code
if (0 < direction < 90) { System.out.println ("Could not recognize direction. Heading initialized to 0"); } i keep getting a "operator < cannot be applied to boolean,int. any help would be appreciated, just doing a simple lab assignment for a cs class and java syntax is smacking me around |
Your compiler is telling you that (0 < direction) evaluates to a boolean so it can't be compared with 90 using <
Does that help? |
yeah, i understood the compiler was telling direction was boolean, but direction isn't boolean, it's int. would posting the entire class help?
|
Sorry my answer was ambiguous, i edited it just as you replied to it... reread it.
It should be (0 < direction AND direction < 90) Or does java use && for AND? I cant remember |
welshbyte, you are my hero, i've been working on this for a friggin hour, and in 5 minutes you got me headed in the right direction. the assignment is done, that's the only part of the code i was having trouble with, thank you so much for your help
|
You're welcome :)
|
perhaps i spoke too soon. when i tried to compile the code, the part that i had thought was correct is giving me a missing return statement error.
Quote:
|
Its just complaining because there's no "default" return.. so if you call getDirection(30) it wont be able to handle it... you just need to add something like
return ""; just between the last 2 braces. |
You do have a missing return... All the returns in the block you pasted occur inside of conditionals. The compiler isn't able to guarentee that any of those conditionals will be executed.
Some soultions... After all the if statements, have something like: return "error!!!Helpme!!"; Or, define String retval; Then, let each if statement say something like retval="east"; and just return retval; at the bottom. Better still. Keep a Map called direction as a member of your class. Code:
direction = new HashMap() |
thanks bacon, i figured it out before you posted, and my cs lab instructor doesn't want us using what we haven't been taught yet (eg, the entire code block you posted, lol), but i appreciate the help, and thank you welsh, once again, for aiding me in my time of need
|
Don't use multiple returns, they are a stylistic nightmare.
use else with your alternate logical paths. && is the logical AND operator. consider the "switch" statement, if your teacher has told you of it. |
All times are GMT -8. The time now is 07:57 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project