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!");
}
}
6 1922
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.
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.
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: - 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!");
-
}
-
}
How about : - 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.println(Arrays.toString(values));
-
-
}
-
-
How about: - 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.println(Arrays.toString(values));
-
-
}
-
-
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
by: eksamor |
last post by:
I have a simple linked list:
struct element {
struct element *next;
int start;
};
struct list {
struct element *head;
|
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...
|
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 "\\".
.... ...
|
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...
|
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...
|
by: tammygombez |
last post by:
Hey fellow JavaFX developers,
I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
|
by: tammygombez |
last post by:
Hey everyone!
I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
| |