Please state the error in following program.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Amiya Maitreya
    New Member
    • Sep 2010
    • 15

    Please state the error in following program.

    Hey ! I am a Semi-pro/Amateur kind of Java Programmer. A student. I use BlueJ to write Java Programs. I was writing a Java Program to display the prime factors of a number entered. However the compiler was constantly showing error of missing return statement. Why ? and can u tell me how to cure it ?

    Code:
    public class Prime_Factorizer
    {
    public static void main(int a)
    {
    final int l = a ;
    System.out.println("Prime Factors of " + a + " are :");
    for(int b = 2 ; b < l ; b++)
    {
    if(a==1)
    {break;}
    int i = Prime_Determine(b);
    
    int q = Divider(a,i);
    a=q;
    }}
    
    static int Prime_Determine(int aa) 
    /*it says itz missing return statement from this function.*/
    {
    if(aa==2)
    return aa;
    else
    {
    for(int bb=2 ; bb <=aa ; bb++)
    {
    int cc = aa%bb;
    if((cc!=0)&&(bb==(aa-1)))
    return aa;
    else if(cc==0)
    return 0;
    }
    }
    }
    
    static int Divider(int dd, int ee)
    /*it says itz missing return statement from this function too !!! :( */
    {
    for(int ff = 1 ; ff > 0 ; ff++)
    {
    int gg = dd%ee ;
    if(gg==0)
    {
    dd = dd/ee ;
    System.out.println(ee);
    }
    else if(gg!=0)
    {
    return dd;
    }}}
    }
    I use BlueJ version 3.0.1 , JDK 6 update 21.
    Kindly help.
    Reply to this thread + if possible email me => {edit} email address removed {/edit}
    Last edited by Stewart Ross; Sep 4 '10, 08:02 PM. Reason: e-mail address removed from post
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Hi there!
    In both cases, you return something only if certain conditions are fulfilled. Now, even if you think of all possible cases, the jre can't know that you have, so it will give you an error. Here, let me give you an example:
    Code:
    public int test(boolean a) {
      if(a)
        return 1;
      else if(!a)
        return 0;
    }
    will give you an error, because the jre has no way of knowing, that you have checked for all possible solutions. However, with a small change it will work:
    Code:
    public int test(boolean a) {
      if(a)
        return 1;
      else // no further if statement here
        return 0;
    }
    Alternatively, you could do something like this:
    Code:
    public int test(boolean a) {
      if(a)
        return 1;
      else if(!a)
        return 0;
      return 42;
    }
    It will never return 42, because as soon as it arrives at a return statement while runtime, it will exit the function. But this way the jre knows, that the function will always return something.

    Greetings,
    Nepomuk

    Comment

    • Amiya Maitreya
      New Member
      • Sep 2010
      • 15

      #3
      Thnx worked lyk magic ...bt now i hv a new error ! plz debug.

      Code:
      public class Prime_Factorizer
      {
      public static void main(int a)
      {
      final int l = a ;
      System.out.println("Prime Factors of " + a + " are :");
      for(int b = 2 ; b < l ; b++)
      {
      if(a==1)
      {break;}
      int i = Prime_Determine(b);
      if(i!=0) 
      int q = Divider(a,i); //it says ".class" expected !! ???
      a=q;
      }}
       
      static int Prime_Determine(int aa) 
      {
      if(aa==2)
      return aa;
      else
      {
      for(int bb=2 ; bb <=aa ; bb++)
      {
      int cc = aa%bb;
      if((cc!=0)&&(bb==(aa-1)))
      return aa;
      else if(cc==0)
      return 0;
      }
      }
      return 23; //obselete
      }
       
      static int Divider(int dd, int ee)
      {
      for(int ff = 1 ; ff > 0 ; ff++)
      {
      int gg = dd%ee ;
      if(gg==0)
      {
      dd = dd/ee ;
      System.out.println(ee);
      }
      else if(gg!=0)
      {
      return dd;
      }}
      return 23; //useless
      }
      }

      Comment

      • Amiya Maitreya
        New Member
        • Sep 2010
        • 15

        #4
        Check line14.

        Comment

        • Dheeraj Joshi
          Recognized Expert Top Contributor
          • Jul 2009
          • 1129

          #5
          I am not sure about that error. But in line 14 you can not declare q only as an integer, it should be final as well. Since your Divider function is static you must have q as final int.

          Code:
          final int q = Divider(a,i);
          a=q;
          Regards
          Dheeraj Joshi

          Comment

          • Amiya Maitreya
            New Member
            • Sep 2010
            • 15

            #6
            but if i put a "final" it will become a constant for the rest of the execution but i want to change it as the loop procedes :( ...any other cure ?

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              Why you want the function to be static?
              I guess it should not be static since the value returned is dependent on the loop execution.

              Regards
              Dheeraj Joshi

              Comment

              • Amiya Maitreya
                New Member
                • Sep 2010
                • 15

                #8
                i hate creatin objects.....hwe ver i wrked out solution 4 the program...if i directly declare

                Code:
                a = Divider(a,i);
                itz wrkx.
                anyways thnx 4 help.

                Comment

                Working...