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 7 of 7

Thread: Problems with my prime finder program

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

    Default 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

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Problems with my prime finder program

    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.
    If you don't understand my answer, don't ignore it, ask a question.

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

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

    Please help me with my program.
    Thanks,
    Truck35

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Problems with my prime finder program

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

    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 {
    If you don't understand my answer, don't ignore it, ask a question.

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

    Question Re: Problems with my prime finder program

    Quote Originally Posted by Norm View Post
    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 View Post
    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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Problems with my prime finder program

    I checked all my curly brackets and they are correct
    One example of poorly formatted code
    		}
    		return counter;
    		}

    The two }s shown above should NOT be in the same column. Nested code should be indented.
    		}
    		return counter;
    	}
    Poorly formatted code is hard to read.

    I don't know what is meant by static method
    This is a static method
    public static void main(String args[])
    See the word static used in its definition.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default 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

Similar Threads

  1. Problem with my prime number finder program
    By Truck35 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 6th, 2012, 10:25 PM
  2. Fixing this grade finder program?
    By Stinger25 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: December 4th, 2012, 07:10 AM
  3. [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, 01:24 PM
  4. Prime Number Program for class
    By chachunga in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 22nd, 2011, 12:05 AM
  5. [SOLVED] Recursive Sentence Finder
    By raphytaffy in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 21st, 2010, 01:46 PM