Results 1 to 8 of 8
- 06-26-2009, 11:28 PM #1
Member
- Join Date
- Jun 2009
- Posts
- 2
- Rep Power
- 0
Another noob first post asking for help
Hey, from what you can probably gather from the title, I have like no experience with java. Only previous programming is from 2 high school courses in visual basic .Net
Anyways, on to the point. I have been reading the java starter guides and decided to try my hand at some practice, so I checked out javabat, and decide to try my hand at one (can't link yet I guess.)
Basically, I need to take a value and determine if it is within 10 of either 100 or 200. Here is my code so far
Now netbeans tells me it compiles ok, but it doesn't, and the error is from this line:Java Code:public class Main { public static void main(String[] args) { int n; n = 45; isNear(n); if(isNear == true){ System.out.println("The value is near 100 or 200"); }else{ System.out.println("The Value is not near 100 or 200"); } } public static boolean isNear(int a){ a = Math.abs(a); if(100 - a <= 10 || 200 - a <= 10 || 110 - a <= 10 || 210 - a <= 10){ return true; }else{ return false; } } }
I guess my question is, how do you compare booleans ? Is there a method I missed ?Java Code:if(isNear == true){
Also, feel free to criticize my sloppy code, I would rather get a good grasp now than a year down the road.
Many thanks.
//Edit; I found the Math.abs method and cleaned it up a little bitLast edited by Dan M.; 06-26-2009 at 11:38 PM.
- 06-27-2009, 12:31 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
A couple of things about this. First it is isNear(n) that is - or at least returns - a boolean. And that whole expression is what should go inside the if condition. Unlike some other langauges Java does not use the "bare" method name isNear for anything much. You will always use it as isNear(someValue).Java Code:if(isNear == true){
Secondly comparing something to true is not really needed. So it's more polished to say
This is just like ordinary language where we might say "If you are near the cliff step back otherwise keep going", rather than the longer "if the assertion that you are near the cliff is true step back...".Java Code:if(isNear(n)) { // etc } else { // etc }
Check lots. (although I see you are now using abs() which is good since 100-1000000<=100)
- 06-27-2009, 12:32 AM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
hmm... you are close, but not quite. If NetBeans is telling you it's ok, then get rid of NetBeans and get a new IDE, such as Eclipse, because that is just wrong.
Take a look at this code. Your problem is that you were treating isNear(int) as a variable after you called it, which is incorrect. Instead, it returns a variable, which can be retrieved using boolean isNear = isNear(n);
See the below code, it works well and is self-explanatory
Java Code:public class Main { public static void main(String[] args) { int n; n = 45; if(isNear(n)){ /* Alternately, the above statement could be * boolean isNear = isNear(n) * if(isNear) * * if(isNear(n)==true) is valid as well, but is * just unnecessary and errorprone. If you wanted * to change the statement to * if(isNear(n)==false) you could use * if(!isNear(n)). With all boolean variables and * methods that return boolean values, * the exclamation mark (!) swaps their value. * For example, (!true == false) would be true, * and (!false == true) would be true; */ System.out.println("The value is near 100 or 200"); }else{ System.out.println("The value is not near 100 or 200"); } } public static boolean isNear(int a){ a = Math.abs(a); return (Math.abs(a-100)<=10||Math.abs(a-200)<=10); //edit to your (unfortunately incorrect) logic } }If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 06-27-2009, 12:37 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
And, by the way, the remark about if(foo==true) being the same as if(foo) has a parallel for return statements that return a boolean value. Suppose I wanted to test if a value was zero - a silly example but it will do. The following are equivalent: the second is preferred.
Java Code:boolean isZero(int toTest) { if(toTest == 0) { return true; } else { return false; } } boolean isZero(int toTest) { return toTest == 0; }Last edited by pbrockway2; 06-27-2009 at 12:39 AM.
- 06-27-2009, 12:42 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
- 06-27-2009, 01:15 AM #6
Member
- Join Date
- Jun 2009
- Posts
- 2
- Rep Power
- 0
Awesome! Thanks a ton pbrockway and Boyo. Taking your guys' suggestions and fixing the logic as pbrockway pointed out, I ended up with
Works like a charm (took out the heavy commenting I had to do)Java Code:public class Main { public static void main(String[] args) { int n; n = 156; if(isNear(n)){ System.out.println("The value is near 100 or 200"); }else{ System.out.println("The Value is not near 100 or 200"); } } public static boolean isNear(int a){ if((10 >= a -100 && -10 <=a-100) || (10>=a-200 && -10<=a-200) ){ return true; }else{ return false; } } }
Thanks again guys.
- 06-27-2009, 01:26 AM #7
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
@pbrockway2: He had the a = Math.abs(a) in there. I assumed he wanted to test if it was near 100/200 OR -100/-200.
@ Dan M: Just glad I could help. The isNear(int) method is still a bit longer than it needs to be (instead of if(statement) return true; it could be return(statement), but since you're new, you'll live.If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 06-27-2009, 11:04 AM #8
Member
- Join Date
- Apr 2009
- Location
- Brisbane
- Posts
- 86
- Rep Power
- 0
My 2c...
A little too succinct for comprehensibility... but I did so "because I can".
Cheers. Keith.Java Code:class NearTest { public static boolean isNear(int a, int b) { return Math.abs(a-b) <= 10; } public static void main(String[] args) { int a = 110; System.out.println(a+" is "+(isNear(a, 100) || isNear(a, 200)?"":"not ")+"near 100 or 200"); } }
Similar Threads
-
DOnt know if 1st post if did, I am VERY sorry for duplicate post. I have error messg
By afisher300 in forum New To JavaReplies: 3Last Post: 05-04-2009, 03:15 AM -
Help im a noob.. a super noob on java..
By critdevil in forum New To JavaReplies: 12Last Post: 03-07-2009, 03:17 AM -
Noob
By nokomis in forum IntroductionsReplies: 2Last Post: 03-06-2009, 05:10 PM -
Question - I'm a noob!
By Insaeno in forum New To JavaReplies: 5Last Post: 08-04-2008, 03:20 AM -
Please help a noob :)
By Bays in forum New To JavaReplies: 15Last Post: 06-17-2008, 06:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks