# Problem with my prime number finder program

• December 2nd, 2012, 05:43 PM
Truck35
Problem with my prime number finder program
I am working on a program that finds a prime number that the user has entered. For example, if the user wants to find the 101st prime number, the program will calculate numbers and determine if they are prime numbers. Every prime number will be kept track of and counted then returned to the user. I came up with the code below but, for some reason I am getting errors. I am getting two errors. One when in the code that returns the value from the "get_prime" method. And one in the method call in my System.out.println statement. The exact error message that I seen when I tried to compile the program is typed in the code as comments above the statemnts. If anyone could help me figure out my problem, I would appreciate it.

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; do{ for (i=2;;){     for (j=2; j <= i/j; j++){ if ((i%j) != 0){ counter++; } } }           }while (num != counter); //Getting the error message "Unreachable code" for this return statement return counter; } } public class PrimeLocator {   /** * This program will ask the user which prime number they want, find it, and return the value. */ 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);   /*Getting the error message "Cannot make static reference to the non-static method get_prime() from the type PrimeWorker*"/   System.out.println("The " + number + "th prime is " + PrimeWorker.get_prime());     } }```
• December 2nd, 2012, 07:01 PM
jps
Re: Problem with my prime number finder program
Quote:

I am getting two errors.
Always post errors and code with your question. Now rather than answering your question, I am asking you one. What are the errors? (exactly)

I took a peek at the code, and noticed a few variables declared at the class level but did not see the need to store their values outside the method they are used in. Visualize a method that takes an int value as a parameter representing the Nth prime to get, and returns an int that is the Nth prime.
• December 3rd, 2012, 08:28 AM
Truck35
Re: Problem with my prime number finder program
I put the error message that I received when trying to compile the program as comments above each piece of code that is giving me problems. One is above my final print statement and one is above the return statement in my "get_prime()" method.

Thanks,
Truck35
• December 3rd, 2012, 01:26 PM
jps
Re: Problem with my prime number finder program
Quote:

"Unreachable code"
Looks like a problem with the curly braces. Try to keep your code formatted according to convention and mistakes like this will be more obvious.

Quote:

"Cannot make static reference to the non-static method get_prime() from the type PrimeWorker*"
You are trying to use a method which will belong to an object without creating an object to use the method of. You will have to create an object of the class to use the methods or make the methods static methods of the class. Most likely you will want to create an object, but there are times when you do not. That is why this is an error and it does not do what you wanted it to do here. What you wanted to do is not obvious to the compiler
• December 6th, 2012, 10:25 PM
Truck35
Re: Problem with my prime number finder program
I am still trying to get this program to work. I checked all my brackets and they seem to be correct. I am using an object for my method call and I am still getting errors. according to what I read in my text book, the program should work. The program is below with the errors as comments.

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". }```