//* This skeleton combines both parts of assignment 4 into one. You can do the same
// * by Serrano
import java.util.*;
public class numbermove {
//various methods used
static Scanner input = new Scanner(System.in);
public static void printArray(int[] array, int size)
{
for(int i=0; i<size; i++)
{
System.out.print(array[i] + " ");
}
System.out.println();
}
public static int getIndex(int[] array, int size, int index)
{
// this method searches for item in array and if found returns its index. Otherwise returns -1
for (int i=0; i<size; i++)
{
if (array[i] == index)
return i;
}
return -1;
}
public static int removeAt(int[] array, int size, int item)
{
// removes from array whatever item is at given index and shifts remaining elements.
//return the updated size
if (item >= size)
{
//print index out of bound. Nothing removed.
return size;
}
//otherwise shift elements to left from the cell whose item was removed
for (int i =item; i<item-1; i++) {
array[i] = array[i+1]; // value in cell i is overwritten by the value in cell i+1
}
return size--;
}
public static int remove(int[] array, int size, int item)
{
//first find if the item is in the array by calling getIndex method
int index = getIndex(array, size, item);
if (index == -1)
{
//print item not found, so nothing removed.
System.out.println("nothing removed:\n");
// without any changes to the size.
return size;
}
else{
return removeAt(array, size, item);
}
}
//otherwise call removeAt to remove that item and update the array and its size
public static void main(String[] args)
{
//declare
final int MAX_SIZE = 5;
int[] array = new int[MAX_SIZE];
int numericalOut;
// prompt user as before to get values for an array
System.out.println("Please input data values as integers:\n");
for (int i=0; i<array.length; i++)
{
array[i] = input.nextInt();
}
// print the array
System.out.println(":/n");
printArray(array,array.length);
//prompt user for an integer to remove from the array and capture it as item.
System.out.println("Please choose which integer too remove:/n");
numericalOut = input.nextInt();
//call remove() method to remove that item.
remove(array, array.length, numericalOut);
//print the modified array
System.out.println("" +
"here is the up dated array:/n");
for (int i=0; i<array.length; i++)
{
System.out.println(array [i] + "");
}
}
}
im lost my code is not removing the interger even thou i call it how can i get it to remove the interger and update the size of the array. Can someone help me please Thank you.

New Topic/Question
Reply




MultiQuote








|