2 Replies Latest reply on Aug 16, 2007 11:36 AM by 807600

    Boolean return type. Help please.

    807600
      i can't figure whats wrong with my isPositive method. It cant display the "true" or "false" after cheking if my nVal is positve or negative.
      import java.util.*;
      
      public class Digits
      {
          public boolean isPositive(int nVal)
          {
               if(nVal>=0)
                    return true;
               else
                    return false;
          }
            public static void main(String[] args)
          {
               int nVal;
               Digits d = new Digits();
               Scanner a = new Scanner(System.in);
               System.out.print("Enter Integer: ");
               nVal = a.nextInt();
               d.isPositive(nVal);
               
          }
      }
        • 1. Re: Boolean return type. Help please.
          807600
          Well, you're not doing anything with the return value. Try
          import java.util.*;
           
          public class Digits
          {
              public boolean isPositive(int nVal)
              {
                   return nVal>=0; // note this simplification, too
               }
                public static void main(String[] args)
              {
                   int nVal;
                   Digits d = new Digits();
                   Scanner a = new Scanner(System.in);
                   System.out.print("Enter Integer: ");
                   nVal = a.nextInt();
                   System.out.println(d.isPositive(nVal));
                   
              }
          }
          • 2. Re: Boolean return type. Help please.
            807600
            thank you so much! :D