473,382 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 2041
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

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

Similar topics

6
by: Jim Bancroft | last post by:
Hi everyone, Could someone reccomend a way to remove duplicate/outmoded nodes in an XML document? I didn't describe that well, so let me give an example. For instance, if I have an XML doc...
4
by: ouch | last post by:
Hi Everyone, Is there any way to remove the "@" from a verbatim string? I've tried the .remove(int, int) method, but it doesn't work. Thanks ouch
3
by: Caroline | last post by:
I have a CF app and I haven't found a decent obfuscator for it. I want to write a simple obfuscator, that replaces variables and methods names. Is there any way to rename all method names returned...
0
by: a | last post by:
I'm trying to delete an item from a collection, by clicking on a delete button in a GridView control. The item in the collection is stored in the Profile object (serialized as xml in the...
4
by: eksamor | last post by:
I have a simple linked list: struct element { struct element *next; int start; }; struct list { struct element *head;
19
by: brasilino | last post by:
Hi Folks: I've been looking (aka googling) around with no success. I need a usability beyond 'pop()' method when removing an Array elements. For example: oName = new...
2
by: Konstantinos Pachopoulos | last post by:
Hi, i have the following string s and the following code, which doesn't successfully remove the "\", but sucessfully removes the "\\". .... if i!="\\": .... newS=newS+i .......
4
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in...
4
by: sebsauvage | last post by:
Hello. I'm using SimpleHTTPServer (work well) but it always sends "Server" header in response: "Server: SimpleHTTP/0.6 Python/2.5.1" How can I remove that ? I tried:
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.