Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 09-14-2004, 08:07 AM   #1 (permalink)
Psycho
 
bacon_masta's Avatar
 
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
bacon_masta is offline  
Old 09-14-2004, 08:10 AM   #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
welshbyte is offline  
Old 09-14-2004, 08:12 AM   #3 (permalink)
Psycho
 
bacon_masta's Avatar
 
Location: i live in the state of denial
yeah, i understood the compiler was telling direction was boolean, but direction isn't boolean, it's int. would posting the entire class help?
bacon_masta is offline  
Old 09-14-2004, 08:13 AM   #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
welshbyte is offline  
Old 09-14-2004, 08:18 AM   #5 (permalink)
Psycho
 
bacon_masta's Avatar
 
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
bacon_masta is offline  
Old 09-14-2004, 08:19 AM   #6 (permalink)
Insane
 
Location: Wales, UK, Europe, Earth, Milky Way, Universe
You're welcome
__________________
There are only two industries that refer to their customers as "users". - Edward Tufte
welshbyte is offline  
Old 09-14-2004, 08:39 AM   #7 (permalink)
Psycho
 
bacon_masta's Avatar
 
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:
private String getDirection()
{
if (degrees == 0) {
return "north";
}
if (degrees == 90) {
return "east";
}
if (degrees == 180) {
return "south";
}
if (degrees == 270) {
return "west";
}
}
i checked everything, and all the return statements are there. another syntax problem? thanks to anyone who feels like checking it out
bacon_masta is offline  
Old 09-14-2004, 08:54 AM   #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
welshbyte is offline  
Old 09-14-2004, 09:02 AM   #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");
...
Then, your method gets reduced to return direction.get(new Integer(degrees));
bacon is offline  
Old 09-14-2004, 09:11 AM   #10 (permalink)
Psycho
 
bacon_masta's Avatar
 
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
bacon_masta is offline  
Old 09-14-2004, 12:21 PM   #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.
manalone is offline  
 

Tags
dumb, java, question

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 03:16 PM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54