0

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!

7
Contributors
9
Replies
11
Views
5 Years
Discussion Span
Last Post by jalpesh_007
0

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.

-1
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;
    }
} 
1

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.

0

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!

0

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.

0

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.