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) {
^
boolean isPrime(int n) {
^
2 errors
``````

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

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.

## All 9 Replies

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
Scanner Prime = new Scanner(System.in);
^
symbol:   class Scanner
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