# Identifying Prime Numbers from a Predefined Array

• 11-05-2012, 04:42 PM
milleniamisc
Identifying Prime Numbers from a Predefined Array
I'm taking a crash course in Java, and one of the assignments after the first day is to create a program that tells you all the prime numbers from a predefined array of numbers (we haven't gotten to scanners yet). I managed to write a program that works, but I've got a gut feeling that I did more than I should, and there's a simpler way. Can anyone guide me as to how (if possible) I can simplify this?

Code:

```public class TestPrime {         public static void main(String[] args) {                 int[] numbers = new int[]{1,2,3,4,5,6,7,8,9,10};                 boolean prime = true;                 for ( int i = 0 ; i < numbers.length ; i++){                         switch (numbers[i]) {                         case 1:                                 System.out.println(numbers[i] + " is a prime number");                                 break;                         case 2:                                 System.out.println(numbers[i] + " is a prime number");                         default:                                 for (int x = 2 ; x < numbers[i] ; x++){                                         if (numbers[i]%x == 0){                                                 prime = false;                                                 break;                                                 }                                         if (prime = true) {                                                 System.out.println(numbers[i] + " is a prime number");                                                 break;                                                 }                                 }                         break;                         }                 }         } }```
The end result in the console looks like:

xx is a prime number.
xx is a prime number.
xx is a prime number.
etc etc.
• 11-05-2012, 07:27 PM
JosAH
Re: Identifying Prime Numbers from a Predefined Array
That second if-statement in the body of that last for loop-statement doesn't belong there (suppose 77 is an array element, it would be marked as being prime); move it after that loop and change 'prime = true' to 'prime == true'. Oh, and 1 isn't a prime number.

kind regards,

Jos
• 11-06-2012, 01:40 AM
milleniamisc
Re: Identifying Prime Numbers from a Predefined Array
Thank you! I didn't realize that what I had before ended up showing me all the odds instead of the primes. But your suggestion along with moving the prime declaration to right after the "default;" line did it. Here's the latest version

Code:

```public class TestPrime {         public static void main(String[] args)                 {                 int[] numbers = new int[]{17,23,53,123,66,3,2,1};                 boolean prime;                 for ( int i = 0 ; i < numbers.length ; i++)                         {                         switch (numbers[i])                                 {                         case 1:                                 System.out.println(numbers[i] + " is a prime number");                                 break;                         case 2:                                 System.out.println(numbers[i] + " is a prime number");                                 break;                         default:                                 prime = true;                                 for (int x = 2 ; x < numbers[i] ; x++)                                         {                                         if (numbers[i]%x == 0)                                                 {                                                 prime = false;                                                 break;                                                 }                                                else continue;                                         }                                 if (prime == true)                                         {                                         System.out.println(numbers[i] + " is a prime number");                                         break;                                         }                                 }                         }                 }```