# Another noob first post asking for help

• 06-26-2009, 11:28 PM
Dan M.
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

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;         }     } }```
Now netbeans tells me it compiles ok, but it doesn't, and the error is from this line:

Code:

`if(isNear == true){`
I guess my question is, how do you compare booleans ? Is there a method I missed ?

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 bit
• 06-27-2009, 12:31 AM
pbrockway2
Code:

`if(isNear == true){`
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).

Secondly comparing something to true is not really needed. So it's more polished to say

Code:

```if(isNear(n)) {     // etc } else {     // etc }```
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...".

Check lots. (although I see you are now using abs() which is good since 100-1000000<=100)
• 06-27-2009, 12:32 AM
Singing Boyo
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
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     } }```
• 06-27-2009, 12:37 AM
pbrockway2
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.

Code:

```boolean isZero(int toTest) {     if(toTest == 0) {         return true;     } else {         return false;     } } boolean isZero(int toTest) {     return toTest == 0; }```
• 06-27-2009, 12:42 AM
pbrockway2
Quote:

Originally Posted by Singing Boyo
Code:

```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 }```

a=Math.abs(a) is wrong here. Consider what happens when a=-101 which is certainly not near 100.
• 06-27-2009, 01:15 AM
Dan M.
Awesome! Thanks a ton pbrockway and Boyo. Taking your guys' suggestions and fixing the logic as pbrockway pointed out, I ended up with
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;         }     } }```
Works like a charm (took out the heavy commenting I had to do)
Thanks again guys.
• 06-27-2009, 01:26 AM
Singing Boyo
@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.
• 06-27-2009, 11:04 AM
corlettk
My 2c...

A little too succinct for comprehensibility... but I did so "because I can".

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");   } }```
Cheers. Keith.