# Prim numbers

• May 29th, 2012, 03:28 PM
krillov
Prim numbers
I got a task to write a java program which can solve some mathematical actions. Every action has to be in separate methods. My problem is with a method which should sort prime numbers between 0 and a given number. The code works without the method but I have to implement into a method anyway and that's the problem. I tried many different ways with array etc but nothing works. I guess it's a very simple and easy task for many of you but I'm totally beginner so I would like to ask for you help. Thanks!

public class prim4 {

public static void main (String[] args) {

int prim=50;
int j;
boolean control;

for (j=1; j<=prim; j++)
{
control=primtal(j);

if (control=false)
{
break;
}
if (control=true)
{
System.out.println(j + " ");
}
}
}

public static boolean primtal (int j)
{
int i;
boolean result;
for (i=2; i < j ;i++ ){
int temp = j%i;
if (temp==0)
{
result=false;
}
}
if(i == j){
{
result=true;
}
return result;
}
}

}
• May 29th, 2012, 04:54 PM
Parranoia
Re: Prim numbers
It always helps to wrap your code in code tags: [ code][/code]
Also, this part isn't done correctly.
Code java:

```if (control=false) { break; } if (control=true) { System.out.println(j + " "); }```

You are assigning true or false to the control variable instead of checking for its value.

Also, just a little helpful bit of advice: when checking for booleans, you don't need to check if they are equal to true or false as you did. An easier, nicer way of writing it would look like this.

Code java:

```if (control) { //This will execute if control is true. }   if (!control) { //This will execute if control is false. }```
• May 30th, 2012, 01:41 PM
krillov
Re: Prim numbers
Thank you! I was trying as you said but it still writes an error message like: ...\prim4.java:42: error: missing return statement. I canīt find out what the problem is.

Code :

```public class prim4 {   public static void main (String[] args) {   int prim=50; int j; boolean control;   for (j=1; j<=prim; j++) { control=primtal(j);   if (!control) { break; }   if (control) { System.out.println(j + " "); } } }   public static boolean primtal (int j) { int i; boolean result; for (i=2; i < j ;i++ ){ int temp = j%i; if (temp==0) { result=false; } } if(i == j){ { result=true; } return result; } } }```
• May 30th, 2012, 02:14 PM
Parranoia
Re: Prim numbers
In your primtal method, the second if statement has an extra set of braces.
• May 31st, 2012, 03:05 PM
krillov
Re: Prim numbers
Thank you. I haven't seen it. I tried again but when i fix a fail there will be an other. It is now: "prim4.java:42: error: variable result might not have been initialized return result; " Do you think this solution/algorithm could work or should I find out an other one?
• May 31st, 2012, 05:12 PM
Parranoia
Re: Prim numbers
That error just means that you never initialized "result". Set it to true or false to begin with.
• May 31st, 2012, 06:03 PM
krillov
Re: Prim numbers
Thanks again! Process completed now and it writes out numbers or not. It depends on if I set "result" to true or false. So I realized that my method doesn't do anything at all just sending back the "result" I set. I tried to change boolean to int instead and send back the number or 0 from the method but it doesn't work either. I guess the for-loop in the beginning of the program is also bad.
• June 9th, 2012, 11:24 AM
krillov
Re: Prim numbers
I fixed it. :)

Code :

```public class prim7 {   public static void main (String[] args) {   int prim=50; int j; boolean control;   for (j=2; j<=prim; j++){ control=primtal(j);   if (control) { System.out.print(j + " "); } } }   static boolean primtal (int j) { for(int i=2;i<j;i++) { if(j%i==0) return false; } return true; }   }```