Hi, I'm having trouble getting age 10 occurrences and then removing age 10 from the list. Then I want to be able to print the list without displaying age 10 in it. I trying to use my bag class implementation to do this successfully. My bag class is below. Any help is appreciated.

Current Output:

Original List of Ages:
5
3
2
10
Age 10 occurs 0
Age 10 is Removed From List
5
3
2
10


Expected Output:

Original List of Ages:
5
3
2
10
2
10
Age 10 occurs 2
Age 10 is Removed From List
5
3
2
2


Program:

import java.util.Scanner;
import edu.colorado.collections.IntArrayBag;
import java.io.* ;

public class BagDemonstration
{
   
   private static Scanner stdin = new Scanner(System.in);
   
   public static void main(String[ ] args) throws IOException
   {
      IntArrayBag ages = new IntArrayBag( );

      getAges(ages);
   }


   public static void getAges(IntArrayBag ages) throws IOException
   {
      int userInput;
      int ageArr[]=new int[100];
      int zcount = 0;
      int j = 0;

	  do
	  {
      System.out.println("Type the ages of your family members: ");
      userInput = stdin.nextInt( );
      
      ages.add(userInput);
         
        if (userInput != -1)
         {
         ageArr[j++] = userInput; 
         }    
      } while (userInput != -1);
       
       
       System.out.println("Original List of Ages:");
       
       for(int p=0;p<j;p++)
       {
       System.out.println(ageArr[p]);
       }
       
       System.out.println("Age 10 occurs ");
       System.out.println("Age 10 is Removed From List");
       
       for (int p=0; p<j; ++p)
       {
         if (ages.countOccurrences(userInput) == 10)
         {    	
       	  zcount++;
       	  System.out.println(zcount);
       	  ages.remove(userInput);
         }
       System.out.println(ageArr[p]);  
       }
    }
}

Bag Class:

package edu.colorado.collections;


public class IntArrayBag implements Cloneable
{

   private int[ ] data;
   private int manyItems; 
   
  
   public IntArrayBag( )
   {
      final int INITIAL_CAPACITY = 10;
      manyItems = 0;
      data = new int[INITIAL_CAPACITY];
   }
      
   public IntArrayBag(int initialCapacity)
   {
      if (initialCapacity < 0)
         throw new IllegalArgumentException
         ("The initialCapacity is negative: " + initialCapacity);
      data = new int[initialCapacity];
      manyItems = 0;
   }
        
   public void add(int element)
   {
      if (manyItems == data.length)
      {  // Ensure twice as much space as we need.
         ensureCapacity((manyItems + 1)*2);
      }

      data[manyItems] = element;
      manyItems++;
   }

   public void addMany(int... elements)
   {
      if (manyItems + elements.length > data.length)
      {  // Ensure twice as much space as we need.
         ensureCapacity((manyItems + elements.length)*2);
      }

      System.arraycopy(elements, 0, data, manyItems, elements.length);
      manyItems += elements.length;
   }

   public void addAll(IntArrayBag addend)
   {
      // If addend is null, then a NullPointerException is thrown.
      // In the case that the total number of items is beyond
      // Integer.MAX_VALUE, there will be an arithmetic overflow and
      // the bag will fail.
      ensureCapacity(manyItems + addend.manyItems);
         
      System.arraycopy(addend.data, 0, data, manyItems, addend.manyItems);
      manyItems += addend.manyItems;
   }   
   
   
   public IntArrayBag clone( )
   { 
      IntArrayBag answer;
      
      try
      {
         answer = (IntArrayBag) super.clone( );
      }
      catch (CloneNotSupportedException e)
      {  
         throw new RuntimeException
         ("This class does not implement Cloneable");
      }
      
      answer.data = data.clone( );
      
      return answer;
   }
   
   public int countOccurrences(int target)
   {
      int answer;
      int index;
      
      answer = 0;
      for (index = 0; index < manyItems; index++)
         if (target == data[index])
            answer++;
      return answer;
   }

   public void ensureCapacity(int minimumCapacity)
   {
      int[ ] biggerArray;
      
      if (data.length < minimumCapacity)
      {
         biggerArray = new int[minimumCapacity];
         System.arraycopy(data, 0, biggerArray, 0, manyItems);
         data = biggerArray;
      }
   }

   
   public int getCapacity( )
   {
      return data.length;
   }

              
   public boolean remove(int target)
   {
      int index; 
       

      for (index = 0; (index < manyItems) && (target != data[index]); index++)
        
         ;
         
      if (index == manyItems)
         
         return false;
      else
      {  
         manyItems--;
         data[index] = data[manyItems];
         return true;
      }
   }
                 
   public int size( )
   {
      return manyItems;
   }
   
  
   public void trimToSize( )
   {
      int[ ] trimmedArray;
      
      if (data.length != manyItems)
      {
         trimmedArray = new int[manyItems];
         System.arraycopy(data, 0, trimmedArray, 0, manyItems);
         data = trimmedArray;
      }
   }
       
   public static IntArrayBag union(IntArrayBag b1, IntArrayBag b2)
   { 
      IntArrayBag answer = new IntArrayBag(b1.getCapacity( ) + b2.getCapacity( ));
      
      System.arraycopy(b1.data, 0, answer.data, 0, b1.manyItems);
      System.arraycopy(b2.data, 0, answer.data, b1.manyItems, b2.manyItems);
      answer.manyItems = b1.manyItems + b2.manyItems;
      
      return answer;
   }
        
}

On line 51 of your main program, you are counting the number of times the last number you entered occurred, and checking to see if that's equal to ten.

Try rewriting the for loop that starts on line 49 as a while loop that continues as long as ages.countOccurrences(10) > 0 and see if that gets you anywhere.

I have everything working except for removing 10 and then printing the list excluding 10. I am trying to do it with a for loop at the end of the program. After I try to remove 10, it still prints out anyway. Any further help is appreciated.

import java.util.Scanner;
import edu.colorado.collections.IntArrayBag;
import java.io.* ;

public class BagofIntegers
{
   
   private static Scanner stdin = new Scanner(System.in);
   
   public static void main(String[ ] args) throws IOException
   {
      IntArrayBag ages = new IntArrayBag( );

      getAges(ages);

   }


   public static void getAges(IntArrayBag ages) throws IOException
   {
      int userInput;
      int ageArr[]=new int[100];
      int j = 0;
      
      
	  do
	  {
      System.out.println("Type the ages of your family members: ");
      userInput = stdin.nextInt( );
      
      ages.add(userInput);
         
        if (userInput != -1)
         {
         ageArr[j++] = userInput; 
         }    
      } while (userInput != -1);
   
       
       System.out.println("Original List of Ages:");
      
       for(int p=0;p<j;p++)
       {
       System.out.println(ageArr[p]);
       }


     System.out.println("Age 10 occurs");

     System.out.println(ages.countOccurrences(10));
    
     System.out.println("Age 10 is Removed From List");
         

          for(int p=0;p<j;p++)
         {
        
         ages.remove(10);
         	
         System.out.println(ageArr[p]);  
         }
       
         
   }
}

I also tried to the code below to remove the number 10 in the list, but the number 10 still prints out.

for(int p=0;p<j;p++)
         {
         if (ageArr[p] == 10)
        
         ages.remove(userInput);
         	
         System.out.println(ageArr[p]);  
         }

Notice: I don't necessarily have to remove the number 10...I want to atleast skip over the number 10s and then print if easier. I just can not find out how to do it correctly!

I can remove the number 10 by using the code below, however, I am not using ages.remove(userInput) like I want to. Is there a way that i can use it?

for(int p=0;p<j;p++)
       {
         if (ageArr[p]!=10)
        {
        System.out.println (ageArr[p]);
        }
       }

Remove all occurrences of 10 before you enter your printing loop. Remember that the remove method gets rid of the element, and returns True if it removed the element successfully.

Then update j to be the new size, and print everything from 0 to j.

Your previous attempts all modify and print the list at the same time. That's very dangerous, especially due to the way your remove method destroys the order of the list.

I didn't quite understand your last post. Can you break it down for me. I'm still playing around with it as we speak.

I have everything working correctly except for removing the number 10 from the list and then printing the list excluding the number 10.

while ( ages.remove(10) )
    j-- ;

Every time you successfully remove 10 from your list, ages.remove() will return True. Therefore, you put it in a while loop to remove elements until it returns false (where it does nothing). When we do remove an element, we need to alter the size of the loop, so that we do not iterate past the elements we're working with.

After doing this, you will be able to go from 0 to j and print everything.

The reason why your loops were not working is because even when you 'remove' the element, you are just replacing it with the last element in your list. Furthermore, you are not updating the size of the list and you are still iterating to the 'removed' element (effectively counting it twice).

Below is what my current output looks like. I tried to follow your instructions the best that I could. I'm not understanding the behavior of the output. Can you help me with this? Thanks for everything so far.

Current Output:

Original List of Ages:
3
10
2
5
10
6
10
2
10
Age 10 occurs
4
Age 10 is Removed From List
3
10
2
5
10

import java.util.Scanner;
import edu.colorado.collections.IntArrayBag;
import java.io.* ;

public class BagofIntegers
{
   
   private static Scanner stdin = new Scanner(System.in);
   
   public static void main(String[ ] args) throws IOException
   {
      IntArrayBag ages = new IntArrayBag( );

      getAges(ages);

   }


   public static void getAges(IntArrayBag ages) throws IOException
   {
      int userInput;
      int ageArr[]=new int[100];
      int j = 0;
      
      
	  do
	  {
      System.out.println("Type the ages of your family members: ");
      userInput = stdin.nextInt( );
      
      ages.add(userInput);
         
        if (userInput != -1)
         {
         ageArr[j++] = userInput; 
         }    
      } while (userInput != -1);
   
       
       System.out.println("Original List of Ages:");
      
       for(int p=0;p<j;p++)
       {
       System.out.println(ageArr[p]);
       }


     System.out.println("Age 10 occurs");

     System.out.println(ages.countOccurrences(10));
     
     
      
     System.out.println("Age 10 is Removed From List");
         
		
     while ( ages.remove(10) )
     {   
     j-- ;
     }
     
     for(int p=0;p<j;++p)
     {
     System.out.println(ageArr[p]);
     }
     
         
   }
}

I don't think with the bag class, I am able to remove the number 10 successfully. I think I should probably just keep it with what I have below. Even though it is incorrectly, it is the only way fori t to work unfortunately.

for(int p=0;p<j;p++)
     {	
     if (ageArr[p] != 10)
     	
     System.out.println(ageArr[p]);
     }

*facepalm* Man, I'm slow today.

Look at your loop. You're reading the variables stored in ageArr. Your bag class is removing the elements just fine. You're just not retrieving anything from it.

Add a get(int index) method to your bag class that grabs data[index] for you. That should get you going in the right direction. Sorry for wasting your time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.