java - Prime Numbers Assistance

I have to write a code, and this is what I have so far:

A prime number is a number that is evenly divisible only by itself and I. For example, the

number 5 is prime because it can be evenly divided only by 1 and 5. The number 6, however,

is not prime because it can be divided evenly by 1, 2, 3, and 6.

Write a method named isPrime, which takes an integer as an argument and returns true if

the argument is a prime number, or false otherwise. Demonstrate the method in a complete

program.

This is the code so far:

``package isprime;import java.util.Scanner;public class IsPrime {public static void main(String[] args) {Scanner userInput = new Scanner(System.in);System.out.println("Write a number.");int num = userInput.nextInt();}public static boolean isPrime(int num) {int isPrime = num % num;if (isPrime !=0 || isPrime != 1)return false;elsereturn true;}}``

Now the issue is I don't know much about how boolean expressions work, so this is why I made an inner method boolean, but it will not pick the user number response in the inner method. This is where I am stuck. Also, there is a message saying the if is redundant? Can anyone assist in getting to the answer, not the answer itself? I won't learn how to do it in the long run.

Part 2:

Now I have to place these prime numbers into a file. I know I have to use PrintWriter, but I am not really sure where to place it. Also, how do create a file, using PrintWriter fileName = new PrintWriter("PrimeNumbers.txt");

This is the new code created:

``package isprime_final;import java.io.PrintWriter;import java.util.Scanner;public class IsPrime_Final{public static void main(String args[]){Scanner fileName = new Scanner(System.in);System.out.println("Name the file for Prime Number list.");String primeList = fileName.nextLine();for (int isPrime = 0; isPrime <= 100; isPrime++) {if (isPrime(isPrime)) {PrintWriter PrimeNumbers = new PrintWriter(System.out);PrintWriter.println(isPrime);}}PrintWriter.close();}public static boolean isPrime(int prime) {if ((prime & 1) == 0) {if (prime == 2) {return true;} else {return false;}}for (int i = 3; (i * i) <= prime; i += 2) {if ((prime % i) == 0) {return false;}}return prime != 1;}}``

``````int isPrime = num % num;
``````

This doesn't accomplish anything. `num % num` is always 0 by definition (remember that `a % b` is the remainder when dividing `a` by `b`).

``````if (isPrime !=0 || isPrime != 1)
``````

This doesn't accomplish anything either because the expression is always true. You're asking if `isPrime` is different from 0, or different from 1. There are three possible cases:

1. `isPrime` is 0. `isPrime != 0` is false, `isPrime != 1` is true, the whole expression is true.
2. `isPrime` is 1. `isPrime != 0` is true, `isPrime != 1`is false, the whole expression is true.
3. `isPrime` is any other value. `isPrime != 0` is true, `isPrime != 1` is true, the whole expression is true.

What you need, to begin with, is a loop to look for divisors. If you can find a a divisor between `2` and `num-1` so that `num % divisor == 0`, then `num` isn't prime. If you can't find one, then it is prime.

You are not calling the Boolean method isPrime from anywhere from your main method. You need to call it first after you read the response from user. Secondly, you are doing

`````` num%num
``````

Which will always gives you true. So it will never go into false condition.

Hope this helps something

Happy learning :)

A prime number is defined `as a number only divisible by itself and 1`.

To determine if a number N is a prime, a naive approach would be to check if the number is divisible by any number between 1 and N.

``````public static boolean isPrime (int num) {
for (int i=2; i<num; i++)
if (num % i == 0) // num is divisible by i
return false; // num is not prime.
return true; // because num passed all tests, num is prime.
}
``````