Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problem with my prime number finder program

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question 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

    //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());
     
     
    	}
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Problem with my prime number finder program

    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.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Problem with my prime number finder program

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

    "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

  5. The Following User Says Thank You to jps For This Useful Post:

    Truck35 (December 6th, 2012)

  6. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question 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.

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

Similar Threads

  1. [SOLVED] Repeating infinite loop in prime number finder
    By dontpanic in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 6th, 2012, 02:24 PM
  2. Prime number solver.
    By Danny123 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: June 4th, 2012, 06:30 AM
  3. display all prime factors of a number
    By mia_tech in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 18th, 2012, 07:55 PM
  4. Prime Number Code Help!
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 7th, 2012, 12:07 AM
  5. Prime Number Program for class
    By chachunga in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 22nd, 2011, 01:05 AM

Tags for this Thread