Results 1 to 3 of 3
  1. #1
    milleniamisc is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default 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;
    			}
    		}
    
    	}
    
    }
    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.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    milleniamisc is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default 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

  1. generating prime numbers...using an array
    By yingyang69 in forum New To Java
    Replies: 7
    Last Post: 03-01-2011, 05:30 AM
  2. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 01:50 AM
  3. prime numbers program
    By i contra i in forum New To Java
    Replies: 9
    Last Post: 01-15-2009, 07:22 AM
  4. Prime numbers
    By tercius in forum New To Java
    Replies: 3
    Last Post: 05-04-2008, 06:05 AM
  5. Prime numbers
    By gapper in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 10:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •