Results 1 to 3 of 3
 11052012, 03:42 PM #1Member
 Join Date
 Oct 2012
 Posts
 7
 Rep Power
 0
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?
Java 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; } } } }
xx is a prime number.
xx is a prime number.
xx is a prime number.
etc etc.
 11052012, 06:27 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,529
 Blog Entries
 7
 Rep Power
 20
Re: Identifying Prime Numbers from a Predefined Array
That second ifstatement in the body of that last for loopstatement 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,
Joscenosillicaphobia: the fear for an empty beer glass
 11062012, 12:40 AM #3Member
 Join Date
 Oct 2012
 Posts
 7
 Rep Power
 0
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
Java 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; } } } }
Similar Threads

generating prime numbers...using an array
By yingyang69 in forum New To JavaReplies: 7Last Post: 03012011, 05:30 AM 
Prime Number  System print all the prime numbers ...
By pinkdreammsss in forum New To JavaReplies: 20Last Post: 04262009, 01:50 AM 
prime numbers program
By i contra i in forum New To JavaReplies: 9Last Post: 01152009, 07:22 AM 
Prime numbers
By tercius in forum New To JavaReplies: 3Last Post: 05042008, 06:05 AM 
Prime numbers
By gapper in forum New To JavaReplies: 3Last Post: 02072008, 10:09 AM
Bookmarks