1

I am new to java (and programming in general) and decided to make a program that checks whether a number is prime or not.

I made a working program:

import java.io.*;

public class PrimeChecker
{
  public static void main (String[] args) throws IOException
  {
    BufferedReader Reader = new BufferedReader (new InputStreamReader (System.in));

    int factor = 2;
    boolean check = true;
    System.out.println("Please input a number.");
    int number = Integer.parseInt(Reader.readLine ());

    if (number == 1)
    {
      System.out.println(number + " is neither prime nor composite.");
    }
    else
    {
      while (factor <= Math.sqrt(number))
      {
        if (number % factor == 0)
        {
          check = false;
          break;
        }
        factor++;
      }
        if (check)
        {
          System.out.println(number + " is a prime number.");
        }
        else
        {
          System.out.println(number + " is a composite number (not prime).");
        }
    }
  }
}

Then I read this, decided to play around with booleans a bit and changed it to this:

import java.io.*;

public class PrimeCheckerv2
{
  public static void main (String[] args) throws IOException
  {
    BufferedReader Reader = new BufferedReader (new InputStreamReader (System.in));

    int factor = 2;
    System.out.println("Please input a number.");
    int number = Integer.parseInt(Reader.readLine ());

    if (number == 1)
    {
      System.out.println(number + " is neither prime nor composite.");
    }
    else
    {
      while (factor <= Math.sqrt(number))
      {
        public boolean isPrime()
        {
          return (!(number % factor == 0));
        }
        factor++;
      }
        if (isPrime)
        {
          System.out.println(number + " is a prime number.");
        }
        else
        {
          System.out.println(number + " is a composite number (not prime).");
        }
    }
  }
}

I believe I did everything as instructed in the previously linked page I read but my new code is getting these 3 errors:

File: C:\Users\Aion\Documents\DrJava\Other\PrimeCheckerv2.java  [line: 19]
Error: Syntax error, insert "VariableDeclarators" to complete LocalVariableDeclaration
File: C:\Users\Aion\Documents\DrJava\Other\PrimeCheckerv2.java  [line: 19]
Error: Syntax error, insert ";" to complete BlockStatements
File: C:\Users\Aion\Documents\DrJava\Other\PrimeCheckerv2.java  [line: 19]
Error: Syntax error, insert ";" to complete Statement

I have looked through many questions on this site looking for something that might explain them but have found nothing. Can anyone tell me why I am getting these errors?

15
  • 3
    Have you looked at line 19? Commented Feb 16, 2015 at 19:30
  • @Zavior It could also be the one before... Commented Feb 16, 2015 at 19:31
  • 1
    You're declaring a method inside a method, you can't do that. You have public boolean isPrime() inside your while-loop inside main Commented Feb 16, 2015 at 19:32
  • Fixed the mistakes mentioned. Still getting errors Commented Feb 16, 2015 at 20:49
  • You're still declaring the isPrime() method inside your main method. Commented Feb 16, 2015 at 21:06

1 Answer 1

3

There are several errors in your code.

First you define a new method inside another. That does not work in java.

   public boolean isPrime()
    {
      return (!(number % factor == 0));
    }
    factor++;
  }

So you have to put it under the rest of the code.

Second: the method must be static because you want to call that method from a static method.

Third: you have to pass parameters to the method:

public static boolean isPrime(int number, int factor) {
    return number % factor != 0;
}

and you have to call it:

if (isPrime(factor, number))

So your class must look like:

public class PrimeCheckerv2 {
    public static void main(String[] args) throws IOException {
        BufferedReader Reader = new BufferedReader(new InputStreamReader(
                System.in));

        int factor = 2;
        System.out.println("Please input a number.");
        int number = Integer.parseInt(Reader.readLine());

        if (number == 1) {
            System.out.println(number + " is neither prime nor composite.");
        } else {
            while (factor <= Math.sqrt(number)) {
                factor++;
                if (isPrime(factor, number)) {
                    System.out.println(number + " is a prime number.");
                } else {
                    System.out.println(number
                            + " is a composite number (not prime).");
                }

            }
        }

    }

    public static boolean isPrime(int number, int factor) {
        return number % factor != 0;
    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

Gaaah you beat me to it. Note: factor++ is not part of the function.
I modified my code and it is now exactly like what you wrote but I am still getting 3 errors. It now wants me to put semicolons in line 32 after isPrime, int number and int factor. Also, is it required that I put the boolean after the function or can I put it before where I declare my variables?
This code will print "___ is a prime number" for every factor tested when number is prime and will print both statements multiple times when number is composite

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.