473,416 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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 ?

Expand|Select|Wrap|Line Numbers
  1. public class Prime_Factorizer
  2. {
  3. public static void main(int a)
  4. {
  5. final int l = a ;
  6. System.out.println("Prime Factors of " + a + " are :");
  7. for(int b = 2 ; b < l ; b++)
  8. {
  9. if(a==1)
  10. {break;}
  11. int i = Prime_Determine(b);
  12.  
  13. int q = Divider(a,i);
  14. a=q;
  15. }}
  16.  
  17. static int Prime_Determine(int aa) 
  18. /*it says itz missing return statement from this function.*/
  19. {
  20. if(aa==2)
  21. return aa;
  22. else
  23. {
  24. for(int bb=2 ; bb <=aa ; bb++)
  25. {
  26. int cc = aa%bb;
  27. if((cc!=0)&&(bb==(aa-1)))
  28. return aa;
  29. else if(cc==0)
  30. return 0;
  31. }
  32. }
  33. }
  34.  
  35. static int Divider(int dd, int ee)
  36. /*it says itz missing return statement from this function too !!! :( */
  37. {
  38. for(int ff = 1 ; ff > 0 ; ff++)
  39. {
  40. int gg = dd%ee ;
  41. if(gg==0)
  42. {
  43. dd = dd/ee ;
  44. System.out.println(ee);
  45. }
  46. else if(gg!=0)
  47. {
  48. return dd;
  49. }}}
  50. }
  51.  
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}
Sep 4 '10 #1

✓ answered by Nepomuk

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:
Expand|Select|Wrap|Line Numbers
  1. public int test(boolean a) {
  2.   if(a)
  3.     return 1;
  4.   else if(!a)
  5.     return 0;
  6. }
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:
Expand|Select|Wrap|Line Numbers
  1. public int test(boolean a) {
  2.   if(a)
  3.     return 1;
  4.   else // no further if statement here
  5.     return 0;
  6. }
Alternatively, you could do something like this:
Expand|Select|Wrap|Line Numbers
  1. public int test(boolean a) {
  2.   if(a)
  3.     return 1;
  4.   else if(!a)
  5.     return 0;
  6.   return 42;
  7. }
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

7 1530
Nepomuk
3,112 Expert 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. public int test(boolean a) {
  2.   if(a)
  3.     return 1;
  4.   else if(!a)
  5.     return 0;
  6. }
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:
Expand|Select|Wrap|Line Numbers
  1. public int test(boolean a) {
  2.   if(a)
  3.     return 1;
  4.   else // no further if statement here
  5.     return 0;
  6. }
Alternatively, you could do something like this:
Expand|Select|Wrap|Line Numbers
  1. public int test(boolean a) {
  2.   if(a)
  3.     return 1;
  4.   else if(!a)
  5.     return 0;
  6.   return 42;
  7. }
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
Sep 5 '10 #2
Thnx worked lyk magic ...bt now i hv a new error ! plz debug.

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class Prime_Factorizer
  3. {
  4. public static void main(int a)
  5. {
  6. final int l = a ;
  7. System.out.println("Prime Factors of " + a + " are :");
  8. for(int b = 2 ; b < l ; b++)
  9. {
  10. if(a==1)
  11. {break;}
  12. int i = Prime_Determine(b);
  13. if(i!=0) 
  14. int q = Divider(a,i); //it says ".class" expected !! ???
  15. a=q;
  16. }}
  17.  
  18. static int Prime_Determine(int aa) 
  19. {
  20. if(aa==2)
  21. return aa;
  22. else
  23. {
  24. for(int bb=2 ; bb <=aa ; bb++)
  25. {
  26. int cc = aa%bb;
  27. if((cc!=0)&&(bb==(aa-1)))
  28. return aa;
  29. else if(cc==0)
  30. return 0;
  31. }
  32. }
  33. return 23; //obselete
  34. }
  35.  
  36. static int Divider(int dd, int ee)
  37. {
  38. for(int ff = 1 ; ff > 0 ; ff++)
  39. {
  40. int gg = dd%ee ;
  41. if(gg==0)
  42. {
  43. dd = dd/ee ;
  44. System.out.println(ee);
  45. }
  46. else if(gg!=0)
  47. {
  48. return dd;
  49. }}
  50. return 23; //useless
  51. }
  52. }
  53.  
  54.  
  55.  
Sep 5 '10 #3
Check line14.
Sep 5 '10 #4
Dheeraj Joshi
1,123 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1. final int q = Divider(a,i);
  2. a=q;
  3.  
Regards
Dheeraj Joshi
Sep 6 '10 #5
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 ?
Sep 6 '10 #6
Dheeraj Joshi
1,123 Expert 1GB
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
Sep 6 '10 #7
i hate creatin objects.....hwever i wrked out solution 4 the program...if i directly declare

Expand|Select|Wrap|Line Numbers
  1. a = Divider(a,i);
  2.  
itz wrkx.
anyways thnx 4 help.
Sep 10 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: PengYu.UT | last post by:
Hi, I run into error with the following program. Would you please help me? Best wishes, Peng struct tag1{}; struct tag2{};
8
by: G Patel | last post by:
I wrote the following program to remove C89 type comments from stdin and send it to stdout (as per exercise in K&R2) and it works but I was hoping more experienced programmer would critique the...
0
by: Rob Dob | last post by:
Can someone please try the following in VS2005 and let me know if you get the same results... -1-Create a new Web Application use C# -2- double click on the default.aspx form and view it in...
2
by: mikelinyoho | last post by:
Does the following program architecture make sense? #include <stdio.h> #include "test1.c" #include "sample.cpp" int main(void){ output();
7
by: Martin Pritchard | last post by:
Hi, Sorry for my ignorance, but I'm a bit new to C++. I've been handed over a C++ app written in VS2002 which I have to convert to VS2005. Apparently it's been written in a C style, but cannot...
10
by: sunny | last post by:
Does this following program implement the factory design.if not what are things that i have to change in order to make this following program to be designed to factory design pattern. ...
3
by: koutoo | last post by:
I have a code that writes to 2 seperate files. I keep getting a "list index out of range" error. The strange part is that when checking the files that I'm writing too, the script has already...
1
by: =?Utf-8?B?SkQ=?= | last post by:
ok, I got a windows xp home edition computer from my friend, it is a custom made one. The windows xp is an OEM product, so microsoft is willing to charge me $59 to tell me how to fix it since its...
4
by: Cirene | last post by:
In my web.config I added <pages enableSessionState="true">. In each of my pages I also added EnableSessionState="True" to the Page declaration. (I didn't think this was necessary, but...) ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.