![]() |
![]() |
#1 (permalink) |
Psycho
Location: i live in the state of denial
|
[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 |
![]() |
![]() |
#2 (permalink) |
Insane
Location: Wales, UK, Europe, Earth, Milky Way, Universe
|
Your compiler is telling you that (0 < direction) evaluates to a boolean so it can't be compared with 90 using <
Does that help?
__________________
There are only two industries that refer to their customers as "users". - Edward Tufte Last edited by welshbyte; 09-14-2004 at 08:12 AM.. Reason: Grammatical mistake |
![]() |
![]() |
#4 (permalink) |
Insane
Location: Wales, UK, Europe, Earth, Milky Way, Universe
|
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
__________________
There are only two industries that refer to their customers as "users". - Edward Tufte |
![]() |
![]() |
#5 (permalink) |
Psycho
Location: i live in the state of denial
|
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
|
![]() |
![]() |
#7 (permalink) | |
Psycho
Location: i live in the state of denial
|
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:
|
|
![]() |
![]() |
#8 (permalink) |
Insane
Location: Wales, UK, Europe, Earth, Milky Way, Universe
|
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.
__________________
There are only two industries that refer to their customers as "users". - Edward Tufte |
![]() |
![]() |
#9 (permalink) |
Upright
|
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() direction.put(new Integer(0), "north"); direction.put(new Integer(90), "east"); ... |
![]() |
![]() |
#10 (permalink) |
Psycho
Location: i live in the state of denial
|
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
|
![]() |
![]() |
#11 (permalink) |
Once upon a time...
|
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.
__________________
-- Man Alone ======= Abstainer: a weak person who yields to the temptation of denying himself a pleasure. Ambrose Bierce, The Devil's Dictionary. |
![]() |
Tags |
dumb, java, question |
|
|