# Problems with my prime finder program

• December 7th, 2012, 09:54 AM
Truck35
Problems with my prime finder program
I am having trouble with my prime finder program. This program allows the user to enter a number than finds the prime of that number. Example, if you enter five it will find the 5th prime (11). I am getting two errors in my program. One in the first statement in the main method and one for final curly bracket which completes the class. I checked all my curly brackets and they seem good. I also checked the syntax of my main method and that also seems good. acording to what I read in my text book, the program should work. could anyone help me figure this out. The program is below with the errors that a got added as comments.

Thanks,
Truck35

Code :

```//PrimeWorker Class class PrimeWorker { int i, num, counter, j;     //PrimeWorer Constructor PrimeWorker (int b) {   num = b;   } /* Get Prime Method is used to calculate prime numbers and keep count of every prime number * that is produced. That number is then compared to the number entered into the * computer by the user and returned. */ int get_prime() {     counter = 0;   for (i=2;;){     for (j=2; j <= i/j; j++){ if ((i%j) != 0){ counter++; } if (counter == num){ break; } } return counter; }               }   public class PrimeLocator {   /** * This program will ask the user which prime number they want, find it, and return the value. */ /*I got the following error message "The method main cannot be declared static, * static methods can only be declared in static or top level type".*/ public static void main(String args[]) throws java.io.IOException { int x;   //Asks the user to enter which prime number they want to find. System.out.println("Please enter number prime you want to find: "); x = (int) System.in.read(); PrimeWorker number = new PrimeWorker (x);   //Displays the prime number the user wanted to find. System.out.println("The " + x + "th prime is " + number.get_prime());       } //Got the following error, "Syntax error, insert "}" to complete class body". }```
• December 7th, 2012, 10:01 AM
Norm
Re: Problems with my prime finder program
Quote:

I am getting two errors in my program
Please copy and paste here the full text of the error messages that show what the error was and where the error happened.
• December 8th, 2012, 06:21 PM
Truck35
Re: Problems with my prime finder program
/*I got the following error message "The method main cannot be declared static,
* static methods can only be declared in static or top level type".*/
public static void main(String args[])
throws java.io.IOException {

And the second error I got was:

//Got the following error, "Syntax error, insert "}" to complete class body".
}

Thanks,
Truck35
• December 8th, 2012, 06:27 PM
Norm
Re: Problems with my prime finder program
Quote:

"The method main cannot be declared static,
* static methods can only be declared in static or top level type"
The message says it all. A static method must be at the top level class or in a static class.

Quote:

Syntax error, insert "}" to complete class body".
Again the error message says what to do. Check that the class has an ending }
All {s should have a }

One problem the code has is its poor formatting. The { and } are not properly aligned. It is hard to see the ending } that goes with the starting {
• December 9th, 2012, 07:22 AM
Truck35
Re: Problems with my prime finder program
Quote:

Originally Posted by Norm
The message says it all. A static method must be at the top level class or in a static class.

Again the error message says what to do. Check that the class has an ending }
All {s should have a }

One problem the code has is its poor formatting. The { and } are not properly aligned. It is hard to see the ending } that goes with the starting {

Quote:

Originally Posted by Norm
I checked all my curly brackets and they are correct. I don't know what is meant by static method. according to my book the program should work. Please help me any way you can.

Thanks,
Truck35

I
• December 9th, 2012, 07:38 AM
Norm
Re: Problems with my prime finder program
Quote:

I checked all my curly brackets and they are correct
One example of poorly formatted code
Code :

``` } return counter; }```

The two }s shown above should NOT be in the same column. Nested code should be indented.
Code :

``` } return counter; }```
Poorly formatted code is hard to read.

Quote:

I don't know what is meant by static method
This is a static method
Code :

`public static void main(String args[])`
See the word static used in its definition.
• December 9th, 2012, 11:56 AM
C++kingKnowledge
Re: Problems with my prime finder program
put ur PrimeLocator class in another new class and make sure u specify it as the main class to be executed.... then call the PrimeWorker class and its methods from PrimeLocator