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!");
}
}
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!");
}
}
Comment