import java.util.*;
public class PrimeNumber {
private static ArrayList<Integer> listOfPrime = new ArrayList<Integer>();
public static void main(String args[]) {
Scanner userInput = new Scanner(System.in);
System.out.print("Please enter a number to checked the list of prime number > ");
//Request user to input number.
String input = userInput.nextLine();
int number = 0;
boolean check = false;
//Check if integer entered is it integer
while(check != true) {
try {
// if input can be parse into integer, shows that its number so can change check into true to exit the while loop
number = Integer.parseInt(input);
check = true;
}
catch(NumberFormatException e) {
System.out.println("Please enter numbers only > ");
input = userInput.nextLine();
}
}
//Check if input enter is 0 or 1, as prime number cannot be those.
if(number < 2) {
System.out.println("Prime number cannot be 0 or 1");
}else {
checkPrime(number);
}
}
//Check for all prime numbers within the number user input.
private static void checkPrime(int input) {
boolean primeValidation = true;
//first for loop to check from small to biggest integer in the input
for(int i = 1; i <= input;i++) {
if(i !=1) {
//Second for loop to check from prime number in the small to big number.
//If the number i is 2 or 3, the loop will exit and add into the list as the default is prime, and no option changes it to not prime number.
for(int x = 2; x < i ; x++) {
if(i%x == 0) {
//If the number is divisable by any number to 0, set it to not prime.
primeValidation = false;
}
}
}
//if the input is either 0 or 1 set it to false and 0 and 1 is not prime
else {
primeValidation = false;
}
if(primeValidation == true) {
listOfPrime.add(i);
}else {
primeValidation = true;
}
}
System.out.println("The list of prime numbers of the number " + input + " are "+listOfPrime);
}
}
-
1\$\begingroup\$ Welcome to Code Review. Please fix your indentation. The easiest way to post core is to paste it into the question editor, highlight it, and press Ctrl-K to mark it as a code block. Also tell us what the code accomplishes — see How to Ask. \$\endgroup\$– 200_successCommented Nov 24, 2018 at 22:34
-
1\$\begingroup\$ Plus you have two " ` " character making your code unable to compile. Otherwise, a small advise: try to look on internet how to find prime numbers (in any language), you chose one of the worst methods and don't comment what's obvious, it make the reading harder (even with code cleaned ). Welcome on Code Review anyway :) \$\endgroup\$– CalakCommented Nov 24, 2018 at 23:52
-
\$\begingroup\$ There is only one even prime number: 2. All other prime numbers are odd. That means you can halve the amount of work you have to do by not bothering to look at even numbers > 2. \$\endgroup\$– rossumCommented Nov 25, 2018 at 9:19
1 Answer
The first thing I'd suggest is break out code segments into functions. This keeps your Main
free of clutter and makes the flow easier to understand.
You don't mention which IDE you're using. If it doesn't have the option for you to format your code according to acceptable guidelines, I would seriously consider changing programs. There are several well established IDE's freely available.
Your, check for prime, algorithm, while naive, could do with some optimization. Check for 0,1, and 2 separately. If the number is bigger than 1 then add 2 to the list. The loop should start at 3 and step by 2. As was pointed out, all primes larger than 2 are odd.
There are much better algorithms for getting a list of prime numbers, not the least of which is the Sieve of Eratosthenes
. With this algorithm it is very easy to make a master list of primes then build the sublist that you want.