Boolean method

Hi everyone!

I am very new to java as you will soon notice. I am currently attending a class in college and I have a small part of my homework assignment that I am stumped on. It states that I must declare a method:

publicstaticboolean isPrime(int num)

ok, it also says that I need to use for loops and such inside it to determine whether or not a number is prime or not.; however, when I code it to do so, it says I need a return statement outside the loops. please take a look at my method and tell me what my mistake is! thanks!

-Jason

publicstaticboolean isPrime(int num)

{

for(int divisor = 2; divisor <= num / 2; divisor++)

{

if(num % divisor == 0)

{

returnfalse;

}

else

{

returntrue;

}

}

}

I get an error underline on the last closing bracket (very bottom) saying missing return statement

[1664 byte] By [Blue_Chimeraa] at [2007-11-27 9:56:45]
# 1

Even if the compiler allowed this, your logic has a major flaw. Do you see what it is?

Walk through your program using the number 5 as an input, a known prime number, and what happens?

Now try this with 9, a known non-prime. What happens?

Message was edited by:

petes1234

petes1234a at 2007-7-13 0:26:56 > top of Java-index,Java Essentials,New To Java...
# 2
hmmmm now that you mention it I do see that =(Correct me if I am wrong but it will not work because of the if /else Thanks for pointing that out... I am very tired.. maybe I will goto sleep and work on it tomorrow!
Blue_Chimeraa at 2007-7-13 0:26:56 > top of Java-index,Java Essentials,New To Java...
# 3
i think that it is not the if / else, but rather the for loop. There is no guarantee that the for loop will run in every situation. Thus there is a chance that a return statement will never be reached. I may be wrong though.
petes1234a at 2007-7-13 0:26:56 > top of Java-index,Java Essentials,New To Java...
# 4
Now if you can see your error, a little tweaking and your method will be good to go. Don't give up. You are very close.
petes1234a at 2007-7-13 0:26:56 > top of Java-index,Java Essentials,New To Java...
# 5
hmmm... I think I see what you mean. Well I feel kind of weird posting on here. I have only started working with Java 3 weeks ago and along with C++ and VB it can get confusing. so far I like Java =)Thanks again for your help petes1234 =)
Blue_Chimeraa at 2007-7-13 0:26:56 > top of Java-index,Java Essentials,New To Java...
# 6
yw and good luck.
petes1234a at 2007-7-13 0:26:56 > top of Java-index,Java Essentials,New To Java...