I'm trying to write a coding that will be able to determine if a number keyed-in is a Prime Number or not.

Here's what I did:

public class PrimeNumber
{
    public static void main(String args[])  
    {
    int n;

    Scanner Prime = new Scanner(System.in);

    System.out.println(" Please enter a number: ");

   n = Prime.nextInt();

    boolean isPrime(int n) {
     if (n%2==0) return false;

     for(int i=3;i*i<=n;i+=2) {

        if(n%i==0)

            return false;
    }
     return true;
}
}
} 

And here's all the errors that appear:

PrimeNumber.java:13: error: ';' expected
    boolean isPrime(int n) {
                   ^
PrimeNumber.java:13: error: ';' expected
    boolean isPrime(int n) {
                         ^
2 errors

Can someone please shed some light on finishing this coding the CORRECT way? Please and thanks in advance!

You can't define methods within methods. Check your braces and code block organization.

Your isPrime() method cannot be contained within the main() method. You have to keep them separate, like this:

public class PrimeNumber
{
    public static void main(String[] args)
    {
        ...
    }

    public static boolean isPrime(int n)
    {
        ...
    }
}

You could then call the method, from main(), by doing a method call like this: isPrime(n);.

Few other things to note:

  1. Your program will say 1 is prime, which it isn't.
  2. Your program will say 2 is not prime, which it is.
  3. Your program will say all odd negative integers are primes, which they aren't.

A couple of checks before if (n%2==0) return false; would fix your method.

public class PrimeNumber
{
    public static void main(String args[]) {
        int n;
        Scanner Prime = new Scanner(System.in);
        System.out.println(" Please enter a number: ");
        n = Prime.nextInt();     
        System.out.println(isPrime(n));
    }    
    static boolean isPrime(int n) {
        if (n%2==0) return false;
        for(int i=3;i*i<=n;i+=2) {
           if(n%i==0)
               return false;
     }
        return true;
    }
} 

subramanya.vl: don't provide custom made code, give them the chance to work their issues out. also, don't provide false solutions.
2 is a primenumber, yet when your code runs, it would return false if one asked whether or not 2 is a prime.

Okay. So I tried subramanya.vl's coding that he provided here, and I STILL got errors.

Here's the coding:

    public class PrimeNumber
    {
    public static void main(String args[]) 
     {
    int n;
    Scanner Prime = new Scanner(System.in);
    System.out.println(" Please enter a number: ");
    n = Prime.nextInt();
    System.out.println(isPrime(n));
    }
    static boolean isPrime(int n) {
    if (n%2==0) return false;
    for(int i=3;i*i<=n;i+=2) {
    if(n%i==0)
    return false;
    }
    return true;
    }
    } 

And here's the 2 ERRORS I still got:

PrimeNumber.java:6: error: cannot find symbol
    Scanner Prime = new Scanner(System.in);
    ^
  symbol:   class Scanner
  location: class PrimeNumber
PrimeNumber.java:6: error: cannot find symbol
    Scanner Prime = new Scanner(System.in);
                        ^
  symbol:   class Scanner
  location: class PrimeNumber
2 errors

Can someone please shed some light on correcting these 2 errors left? Thanks in advance, anyone!

You need to import the Scanner class.
ps
DaniWeb Member Rules include:
"Do not hijack old threads by posting a new question as a reply to an old one"
http://www.daniweb.com/community/rules
Please start your own new thread for your question

you just have to add the library of scanner class.
I think you have to import java.util. library to remove that error.

please mark this thread solved if you get correct output or your doubts are cleared.

Thanks.

add the library of Scanner class? it's in the core api's, so no, just importing the class 'll do the trick just fine.

Means have to add "import java.util.*"
because Scanner class is defined in util package.So just import it.

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.