Hi there,
I am very, very new to programming and need to work out how to determine if a number entered by a user is a prime number. I have coded this but it does not work. :'(
Any assistance would be greatly appreciated!

I also get the following error even though I do have a main method;

java.lang.NoSuchMethodError: main
Exception in thread "main" Java Result: 1

public static void primeNumberChecker()
        
{
    Scanner keyboard = new Scanner(System.in);
        int inputInt;
        int number = 0;
        int factor = 0;
        double highestPossibleFactor = Math.sqrt(number);
        int i;
        
        System.out.println("Enter an integer value: ");
        
        Scanner console = new Scanner (System.in);
        number = console.nextInt();
        
         for (i=2; i < number ;i++ ){
         int n = number%i;
         
         if (n==0){
         System.out.println("not Prime!");
        break;
  }
  }
                      
         if(i == number){
         System.out.println("Prime number!");
  }
         }
         

    
}
      
  
   public static void main(String[] args) 
{
    primeNumberChecker();
    
    
}
    
    }

you need to remove one '}' right before your main method.
you've closed your class before you started your main method, which is why it can't be found.

there were some errors that i have corrected in your code....and also i have tried to optimize the code.....

import java.util.Scanner;
class prime
{
public static void primeNumberChecker()
 
{
    Scanner keyboard = new Scanner(System.in);
        int inputInt;
        int number = 0;
        int f = 0;
        int i;
 
        System.out.println("Enter an integer value: ");
 
        Scanner console = new Scanner (System.in);
        number = console.nextInt();
 int highestPossibleFactor = (int)Math.sqrt(number);
       
if(number<2)
{
System.out.println("Neither prime nr compsite");
System.exit(0);
} 

         
for (i=2; i <= highestPossibleFactor ;i++ ){
         int n = number%i;
 
         if (n==0){
 f=1;       
        break;
  }
  
 
        else{
f=0;
 //        System.out.println("Prime number!");
  }
         }
if(f==1)
{
 System.out.println("not Prime!");
}
else
{
 System.out.println("Prime!");
}

}
    public static void main(String[] args) 
{
    primeNumberChecker();
 
 }
 
    }
commented: You'll help a lot more by explaining what needs to be done and letting him write it than by dumping code on him -2

there were some errors that i have corrected in your code....and also i have tried to optimize the code.....

could have fooled me .. as far as I've checked his code, by just removing the one closing bracket, problem solved and program works.
so, why adding this answer?
you didn't tell him WHAT errors you've found
you didn't tell WHAT you corrected

basically, you just provided a snippet of code to show off how well you can write code. if you had tested the original code and sollution I provided, you would 've noticed that your post is not only redundant, it sure is not the easiest or fastest sollution for the OP

@IIM
Please be aware that we do not do people's homework for them. By supplying the OP with a "corrected and optimised" solution you are simply inviting him to copy/paste the whole thing and learn nothing (except maybe that cheating is easier than learning).
On the other hand, this is a pretty poor example of a Java program*, so maybe the OP will be smart enough not to embarrass himself by submitting it and getting a low grade.

* using an undocumented int (f) for a boolean value, no error checking, redundant else clause, zero comments, random indentation

Sorry guys i am new to daniweb...So let me put my points abut errors:-
1. Using an optimized method by not checking whether the number is divisible till one less than number,instead using the concept that is number is prime,it will nt be divible by any numbers between 2 and square root of the number.
2. Either you can also do this just by removing an extra '}' in line 32.
3. //double highestPossibleFactor = Math.sqrt(number);
He already used this,so it means he is aware that highest possible factor will be the square root of number...

please suggest, do not disgrade or disrespect others work....
atleast the person tried to solve and solved. if you had something to edit , could have mentioned , why to go harsh..

please suggest, do not disgrade or disrespect others work....
atleast the person tried to solve and solved. if you had something to edit , could have mentioned , why to go harsh..

I think you meant do not "degrade". And they aren't being harsh, they are explaining the difference between being handed the answer and learning how to solve the problem.

1. Using an optimized method by not checking whether the number is divisible till one less than number,instead using the concept that is number is prime,it will nt be divible by any numbers between 2 and square root of the number.

No, a prime number is divisible only by one and itself. So the OP must check all numbers between 2 and half of the "possible prime number" to see if any of those are divisible. There may be other tricks that can be used, but that's the general idea.

Hi BJSJC
The square root answer is the correct one. If "half the possible prime" is a factor, then so will 2 be - which has already been checked. Ditto 1/3 the possible prime, which is only a factor is 3 is (ditto). Once you go bigger than the square root then the other factor must be less than the square root, all of which values have been checked.

Hi BJSJC
The square root answer is the correct one. If "half the possible prime" is a factor, then so will 2 be - which has already been checked. Ditto 1/3 the possible prime, which is only a factor is 3 is (ditto). Once you go bigger than the square root then the other factor must be less than the square root, all of which values have been checked.

*blush*

Of course, you're correct, though it eluded me when I posted earlier. I'm leaving my original post intact so as to not make things even more confusing. Thanks for clearing that up James.

@all-That's why i told this concept of square root to find the prime number....As if you go for large number ,this method will just help to find whether a number is prime or not quicker.

Hi, I just wanted to sincerely thank you for helping with this problem. I'm not sure what I did, but trying it again today it now works. It was very helpful reading your discussions about this.
Oh, and the indentation is mostly due to bad copy/paste as it looks different in NetBeans. As this is only my 5th week studying Java/programming or anything of this kind, I don't feel embarrassed submitting mistakes, I am well aware I have no idea what I am doing and these concepts are a real struggle for me, but thanks for the encouragement....
Just as a side note, I am a 'she'.
Thanks again.

@chicken-If your problem is soved mark the thread as solved....click here to mark it solved...

Coming in late to the game, I just want to point out to the original poster that proving to yourself that you don't need to check past the square root is a worthwhile exercise, and not a tremendously difficult one.

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.