469,902 Members | 1,910 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,902 developers. It's quick & easy.

Need "remove all" method for an assignment

jlandbw04
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.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]);

intListVector.removeElementAt(location);

System.out.print(intListVector + " ");
}
else
System.out.println("Integer not found!");
}
}
Jun 24 '07 #1
6 1716
r035198x
13,262 8TB
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.
Jun 25 '07 #2
r035198x
13,262 8TB
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.
Jun 25 '07 #3
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:

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3. import java.util.Vector;
  4.  
  5. public class chapt10exercise5
  6. {
  7.     static Scanner console = new Scanner(System.in);
  8.     static final int arraySize = 12;    
  9.     public static void main (String[] args)
  10.     {
  11.         int[] intlist = new int[arraySize];
  12.         int index, removeItem;
  13.  
  14.         System.out.println("Please enter " + arraySize + " integers: ");
  15.         for (index = 0; index < arraySize; index++)
  16.             intlist[index] = console.nextInt();
  17.  
  18.         for (index = 0; index < intlist.length; index++)
  19.         System.out.print(intlist[index] + " ");
  20.  
  21.         System.out.print("\n" + "Enter item to be removed: ");
  22.             removeItem = console.nextInt();
  23.  
  24.         removeAll(intlist, arraySize, removeItem);            
  25.     }
  26.  
  27.     public static void removeAll(int[] intlist, int listLength, int searchItem)
  28.     {
  29.         int location;
  30.         boolean found = false;
  31.  
  32.         for (location = 0; location < listLength; location++)
  33.                 if (intlist[location] == searchItem)
  34.                 {
  35.                     found = true;
  36.                     break;
  37.                 }
  38.  
  39.         if (found)
  40.         { 
  41.             int index;
  42.             Vector<Integer> intListVector = new Vector<Integer>();
  43.             for (index = 0; index < intlist.length; index++)
  44.                     intListVector.addElement(intlist[index]);
  45.  
  46.             for (index = 0; index < intListVector.size(); index++)
  47.                     intListVector.removeElementAt(location);
  48.  
  49.             System.out.print(intListVector + " ");
  50.         }    
  51.         else
  52.             System.out.println("Integer not found!");
  53.     }
  54. }
Jun 27 '07 #4
r035198x
13,262 8TB
How about:

Expand|Select|Wrap|Line Numbers
  1. public static void removeAll(int[] intlist, int listLength, int searchItem) {
  2.     Integer[] items = new Integer[intlist.length];
  3.     int nulls = 0;
  4.     for(int i = 0; i < intlist.length; i++) {
  5.         items[i] = intlist[i];
  6.     }
  7.     for(int i = 0; i < items.length;i++) {
  8.         if(items[i] == searchItem) {
  9.             items[i] = null;
  10.             nulls++;
  11.         }
  12.     }
  13.  
  14.     int distinct = items.length - nulls;
  15.     int[] values = new int[distinct];
  16.     int index = 0;
  17.     while(index < distinct) {
  18.         for(int i = 0; i < items.length;i++) {
  19.             if(items[i] != null) {
  20.                 values[index++] = items[i];
  21.             }
  22.         }
  23.     }
  24.     System.out.println(Arrays.toString(values));
  25.  
  26. }
  27.  
  28.  
Jun 27 '07 #5
How about:

Expand|Select|Wrap|Line Numbers
  1. public static void removeAll(int[] intlist, int listLength, int searchItem) {
  2.     Integer[] items = new Integer[intlist.length];
  3.     int nulls = 0;
  4.     for(int i = 0; i < intlist.length; i++) {
  5.         items[i] = intlist[i];
  6.     }
  7.     for(int i = 0; i < items.length;i++) {
  8.         if(items[i] == searchItem) {
  9.             items[i] = null;
  10.             nulls++;
  11.         }
  12.     }
  13.  
  14.     int distinct = items.length - nulls;
  15.     int[] values = new int[distinct];
  16.     int index = 0;
  17.     while(index < distinct) {
  18.         for(int i = 0; i < items.length;i++) {
  19.             if(items[i] != null) {
  20.                 values[index++] = items[i];
  21.             }
  22.         }
  23.     }
  24.     System.out.println(Arrays.toString(values));
  25.  
  26. }
  27.  
  28.  
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.
Jun 29 '07 #6
r035198x
13,262 8TB
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.
Jun 29 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Jim Bancroft | last post: by
4 posts views Thread by ouch | last post: by
3 posts views Thread by Caroline | last post: by
4 posts views Thread by eksamor | last post: by
2 posts views Thread by Konstantinos Pachopoulos | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.