Results 1 to 8 of 8
  1. #1
    Dan M. is offline Member
    Join Date
    Jun 2009
    Posts
    2
    Rep Power
    0

    Default 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

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

    Java 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
    Last edited by Dan M.; 06-27-2009 at 12:38 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Java 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

    Java 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)

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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!

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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 01:39 AM.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by Singing Boyo View Post
    Java 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.

  6. #6
    Dan M. is offline Member
    Join Date
    Jun 2009
    Posts
    2
    Rep Power
    0

    Default

    Awesome! Thanks a ton pbrockway and Boyo. Taking your guys' suggestions and fixing the logic as pbrockway pointed out, I ended up with
    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;
            }
    
        }
    }
    Works like a charm (took out the heavy commenting I had to do)
    Thanks again guys.

  7. #7
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    @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!

  8. #8
    corlettk is offline Member
    Join Date
    Apr 2009
    Location
    Brisbane
    Posts
    86
    Rep Power
    0

    Default

    My 2c...

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

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

Similar Threads

  1. Replies: 3
    Last Post: 05-04-2009, 04:15 AM
  2. Help im a noob.. a super noob on java..
    By critdevil in forum New To Java
    Replies: 12
    Last Post: 03-07-2009, 04:17 AM
  3. Noob
    By nokomis in forum Introductions
    Replies: 2
    Last Post: 03-06-2009, 06:10 PM
  4. Question - I'm a noob!
    By Insaeno in forum New To Java
    Replies: 5
    Last Post: 08-04-2008, 04:20 AM
  5. Please help a noob :)
    By Bays in forum New To Java
    Replies: 15
    Last Post: 06-17-2008, 07:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •