I am trying to make a program that displays the nth prime number. I don't get compiler errors but it doesn't display the prime number. I don't know where it stops, ive tried breakpoints but it stilll doesnt work :(.
here is the code:

package primenumberfinder;
import java.util.Scanner;

/**
 *
 * @author Toby
 */
public class PrimeNumberFinder {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int userChoice;
          int place = 0;

          //sets users choice, if uc is 2 then second prime number
        System.out.println("Which prime number do you want? ");
        Scanner scanner = new Scanner(System.in);
        userChoice =scanner.nextInt();


        for(int i = 0;i <= userChoice;){


            for (int n = i;;n++){

                int checker = 0;

                for(int c = n;;c--){
                    //checks to see if c== o, else, checks if n/c has remainder
                    //if yes checker++
                   if (c==0){
                       break;


                    }else if((n % c) != 0 ){
                        checker++;}


                   }
                //when checker  == n, it should be a prime number.  
                //Itll add this prime number to place
                if (checker == n){
                       place = n;
                       break;
                }

            }
            // increments i and loops through to create the next prime 
            i++;

        }

        //prints prime number
        System.out.println(place);
    }
}

add some print statements in your application. maybe that'll help you figure out where you're going wrong.

Oho A lot of code for such a small application. U better check online examples for Prime Number Finder.
Your problem lies in 2nd Loop it doesnt end because u dont have a condition check. So it moves towards infinite loop.

Majestics: there is a break statement in there, but whether it's reached, that's the question.

The code is not intuitive at all; besides, there are "for" loop in place of "while" loop... Not a good practice...

Anyway, the break statement will stop only the inner most loop, but will not stop the infinite loop which is from the "n" variable. Why? Because "checker" variable will not equal to "n" again once n>0 and there is no upper limit for n to stop the loop... If you still don't get it, try stultuske method to see what happens...

Suggestion
1)Implement finding a prime number first! Don't attempt to think about nth prime number. There will be many smaller steps for this part.
2)Once you get the 1st part right, then move on to find how to get to the nth prime number. Then you should get an idea what actually should be done.

Note
Prime number starts from 2, not 0 or 1...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.