Need "remove all" method for an assignment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jlandbw04
    New Member
    • Jun 2007
    • 3

    #1

    Need "remove all" method for an assignment

    Okay. Here's the deal. I have this assignment for college that has me completely puzzled. I need this assignment to do the following:

    1. input 12 integers into an array from the user.
    2. output the numbers back to the screen showing what the user input.
    3. ask the user what integer they would like to remove.
    4. use a loop to remove ALL occurrance of that integer, and the output the new numbers back to the screen for the user to see the change.
    5. if the integer is not found, display an appropriate message to the screen.

    My program so far does all of those things EXCEPT remove multiple occurrances. My program will run and remove the first occurrance, but none of the duplicates. Any suggestions?

    Here is the code:

    import java.io.*;
    import java.util.*;
    import java.util.Vecto r;

    public class chapt10exercise 5
    {
    static Scanner console = new Scanner(System. in);
    static final int arraySize = 12;
    public static void main (String[] args)
    {
    int[] intlist = new int[arraySize];
    int index, removeItem;

    System.out.prin tln("Please enter " + arraySize + " integers: ");
    for (index = 0; index < arraySize; index++)
    intlist[index] = console.nextInt ();

    for (index = 0; index < intlist.length; index++)
    System.out.prin t(intlist[index] + " ");

    System.out.prin t("\n" + "Enter item to be removed: ");
    removeItem = console.nextInt ();

    removeAll(intli st, arraySize, removeItem);
    }

    public static void removeAll(int[] intlist, int listLength, int searchItem)
    {
    int location;
    boolean found = false;

    for (location = 0; location < listLength; location++)
    if (intlist[location] == searchItem)
    {
    found = true;
    break;
    }

    if (found)
    {
    int index;
    Vector<Integer> intListVector = new Vector<Integer> ();
    for (index = 0; index < intlist.length; index++)
    intListVector.a ddElement(intli st[index]);

    intListVector.r emoveElementAt( location);

    System.out.prin t(intListVector + " ");
    }
    else
    System.out.prin tln("Integer not found!");
    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Code tags code tags code tags
    2.) You don't have to pass the size of the array around since list.length can give you that.
    3.) You realize of course that you are removing only one item in your code. You should do the removing inside a loop (so you remove more than once if necessary). Go for it and let's see if you can get the loop right.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      OK, I've seen your PM. You are not supposed to be using a PM for technical discussions. Repeat your PM post here and we can continue from there.

      Comment

      • jlandbw04
        New Member
        • Jun 2007
        • 3

        #4
        Okay. I tried removing inside of a loop, and now something else is happening. The loop removes the multiples, but it also takes out some other numbers as well. Any suggestion as to why this is happening? Here is the new code:

        Code:
        import java.io.*;
        import java.util.*;
        import java.util.Vector;
        
        public class chapt10exercise5
        {
        	static Scanner console = new Scanner(System.in);
        	static final int arraySize = 12;	
        	public static void main (String[] args)
        	{
        		int[] intlist = new int[arraySize];
        		int index, removeItem;
        		
        		System.out.println("Please enter " + arraySize + " integers: ");
        		for (index = 0; index < arraySize; index++)
        			intlist[index] = console.nextInt();
        		
        		for (index = 0; index < intlist.length; index++)
        		System.out.print(intlist[index] + " ");
        		
        		System.out.print("\n" + "Enter item to be removed: ");
        			removeItem = console.nextInt();
        		
        		removeAll(intlist, arraySize, removeItem);			
        	}
        	
        	public static void removeAll(int[] intlist, int listLength, int searchItem)
        	{
        		int location;
        		boolean found = false;
        		
        		for (location = 0; location < listLength; location++)
        				if (intlist[location] == searchItem)
        				{
        					found = true;
        					break;
        				}
        				
        		if (found)
        		{ 
        			int index;
        			Vector<Integer> intListVector = new Vector<Integer>();
        			for (index = 0; index < intlist.length; index++)
        					intListVector.addElement(intlist[index]);
        								
        			for (index = 0; index < intListVector.size(); index++)
        					intListVector.removeElementAt(location);
        					
        			System.out.print(intListVector + " ");
        		}	
        		else
        			System.out.println("Integer not found!");
        	}
        }

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          How about:

          [CODE=java]public static void removeAll(int[] intlist, int listLength, int searchItem) {
          Integer[] items = new Integer[intlist.length];
          int nulls = 0;
          for(int i = 0; i < intlist.length; i++) {
          items[i] = intlist[i];
          }
          for(int i = 0; i < items.length;i+ +) {
          if(items[i] == searchItem) {
          items[i] = null;
          nulls++;
          }
          }

          int distinct = items.length - nulls;
          int[] values = new int[distinct];
          int index = 0;
          while(index < distinct) {
          for(int i = 0; i < items.length;i+ +) {
          if(items[i] != null) {
          values[index++] = items[i];
          }
          }
          }
          System.out.prin tln(Arrays.toSt ring(values));

          }

          [/CODE]

          Comment

          • jlandbw04
            New Member
            • Jun 2007
            • 3

            #6
            Originally posted by r035198x
            How about:

            [CODE=java]public static void removeAll(int[] intlist, int listLength, int searchItem) {
            Integer[] items = new Integer[intlist.length];
            int nulls = 0;
            for(int i = 0; i < intlist.length; i++) {
            items[i] = intlist[i];
            }
            for(int i = 0; i < items.length;i+ +) {
            if(items[i] == searchItem) {
            items[i] = null;
            nulls++;
            }
            }

            int distinct = items.length - nulls;
            int[] values = new int[distinct];
            int index = 0;
            while(index < distinct) {
            for(int i = 0; i < items.length;i+ +) {
            if(items[i] != null) {
            values[index++] = items[i];
            }
            }
            }
            System.out.prin tln(Arrays.toSt ring(values));

            }

            [/CODE]
            Okay. Where do I put the message "Integer not found" if the integer is not part of the array? I can't figure that out. I've tried placing it in all sorts of places. I either put it in a loop and it repeats itself, or it displays at the wrong time altogether.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by jlandbw04
              Okay. Where do I put the message "Integer not found" if the integer is not part of the array? I can't figure that out. I've tried placing it in all sorts of places. I either put it in a loop and it repeats itself, or it displays at the wrong time altogether.
              You 're playing the guessing game then. That won't help.
              The first thing you have to do is to understand that code.

              Comment

              Working...