#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Stuck: Excluding an Element from an Array


    Hi

    Complete noob to Java. I have an array, elements 1,2,3,4,5
    What I want to do is exclude element 3 in my results but print out 1,2,4,5 and add up 1 to 5 at the end. I'm a bit stuck here, everything I've been trying just wont work. Would really appreciate help!

    Code:
    public class arrays {
    	
    	public static void main(String[] args)
    	{
    		int[] myArray = {1, 2, 3, 4, 5};
    		int total = 0;
    
    for(int i = 0; i < 5; i++)
    					
    		{
    		System.out.println(": " + myArray[i]);
    		}	
    		for (int i = 0; i <5;  i++){
    			total += myArray[i];
    		}
    	
    		System.out.println("The total value is: " + total);
    Last edited by purpur; February 25th, 2013 at 06:57 AM. Reason: Corrections
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    Please use code-tags.

    The array numberArray is not defined, I assume you mean myArray?

    What do you mean with excluding element 3? The value at position 3 in the array, or the value 3 in the array?

    try this for the value at position 3 in the array:

    Code:
    for(int i = 0; i < 5; i++) {
        if(i==2) { // because you start counting at 0 in an array
            break;
        }
        System.out.println(": " + myArray[i]);
    }
    try this for the value 3 in the array:

    Code:
    for(int i = 0; i < 5; i++) {
        if(myArray[i]==3) {
            break;
        }
        System.out.println(": " + myArray[i]);
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Code:
    for(int i = 0; i < 5; i++) {
        if(i==2) { // because you start counting at 0 in an array
            break;
        }
        System.out.println(": " + myArray[i]);
    }
    I still cant get it to display the value at position 4 and the rest of the tray, just leaving out the value at position 3.

    Current Output:
    1
    2

    Required Output:
    1
    2
    4
    5

    What am I doing wrong?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    Stupid me!!! I ment:

    Code:
    for(int i = 0; i < 5; i++) {
        if(i==2) { // because you start counting at 0 in an array
            continue;
        }
        System.out.println(": " + myArray[i]);
    }
    Alternatively you can also do:

    Code:
    for(int i = 0; i < 5; i++) {
        if(i==2) { // because you start counting at 0 in an array
            
        }
        else{
            System.out.println(": " + myArray[i]);
        }
    }
    or:

    Code:
    for(int i = 0; i < 5; i++) {
        if(i!=2){
            System.out.println(": " + myArray[i]);
        }
    }

    Comments on this post

    • purpur agrees : Very helpful
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Ahhhhhh!!!! Thanks so much!!

    Worked a charm. Cheers for the help!

IMN logo majestic logo threadwatch logo seochat tools logo