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

Thread: Prime Number Code Help!

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Prime Number Code Help!

    Alright, I am working with methods for the first time and need help, for some reason my program is not returning the right answer to tell if the number is prime or not, it is part of a larger code (thats why it starts out with an if statement)

    Main:
     if (inputi == 1){
                //Grabs a number converts it than sends it to the method
               int exit = 0;
                do {
                String enter = JOptionPane.showInputDialog("Enter a number: ");
                int enteri = Integer.parseInt(enter);
                boolean kickback = isNotPrime(enteri);
     
                if (kickback = true){
                    output = enteri + " is not a prime number!";
                }
                else {
                    output = enteri + " is a prime number!";
                    exit = 1;
                }
     
               }while (exit > 1);

    isNotPrime Method:
    //Method for not prime numbers
        public static boolean isNotPrime(int number){
            boolean prime = true;
            for (int i = 2; i < number; i++) {
                if (number % i == 0) {
                    prime = false;
                    break;
                }
     
            }
            return prime;
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Prime Number Code Help!

    or some reason my program is not returning the right answer
    Can you show the code's results for different inputs?
    Are you sure you have a valid algorithm for the code?
    If so, then try debugging it by adding printlns to see why the code is not doing what you want it to do.
    Print out the values of the variables as the code executes.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Prime Number Code Help!

    Well:

    if (number % i == 0) {
                    prime = false;
                    break;
                }
    If this condition is given, that means the number is NOT primer, because it has another dividend other than 1 or itself.
    So by making prime = false given this condition, and then later you print it is a primer number, you have a mistake there.

  4. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Prime Number Code Help!

    look very carefull on your code, like what norm had said, are you sure you have the correct algorithm?

    Prime number - Wikipedia, the free encyclopedia

    your method only checks a divisibility, like if the number is even or odd

Similar Threads

  1. Need help correcting my code for calculating sexy prime pairs
    By BinaryPenguin14 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 23rd, 2011, 04:05 AM
  2. Help with code that prints number of evens, odds, and 0's
    By trogan234 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2011, 06:50 PM
  3. 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
  4. [SOLVED] Help with code, Says number is NaN and i cant format it
    By NewGuy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 4th, 2010, 08:28 PM
  5. Need Help - Factoring & Prime Finding Code
    By prodigytoast in forum Algorithms & Recursion
    Replies: 5
    Last Post: November 5th, 2009, 07:38 AM