0

The user enters a value, and it is told that we are to remove the value, and print the newly created array. How do i write that code? It is supposed to be in that method removeValue. Can you also explain how it works.

import java.util.*;

public class ArrayHandler {
  public static void main(String[]args)
  {

    int[] array= new int[5]; // populates Array with 5 numbers
    for(int i=0;i<array.length;i++) // creates the 5 random numbers
    array[i]=(int)(Math.random()*5000);
    printList(array); // prints Array
    System.out.println("");
    System.out.println("Sum: " + calculateSum(array));
    System.out.println("Max: " + findMax(array));
    System.out.println("Minimum: " + findMin(array));
    System.out.println("Value: " + search(array));
  }
  public static void printList(int[] nums)
  {
    for(int i=0; i<nums.length;i++) {
      System.out.print(nums[i] + " "); 
  }
  }
  public static int calculateSum(int[] nums)
  {
    int sum = 0;
    for(int i=0;i<nums.length;i++)
    {
      sum = sum + nums[i];
    }
    return sum;
  }
  public static int findMin(int[] nums)
  {
    int min = nums[0];
    for(int i=0;i<nums.length;i++)
    {
      if(nums[i] < min)
         min=nums[i];
         }
return min;
}
  public static int findMax(int[] nums)
  {
    int max=nums[0];
    for(int i=0;i<nums.length;i++)
    {
      if(nums[i] > max)
        max=nums[i];
    }
    return max;
  }
  public static int search(int[] nums)
  {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("What value do you want?");
    int value = keyboard.nextInt();
    for(int i=0;i<nums.length;i++)
    {
      if(nums[i] == value)
      return i;
   }
    return -1;
  }
  public static int removeValue (int[] nums)
  {
    int[] array = new array[array.charAt(value) - array.charAt(value)];
    if(nums[i] == i)
      return array;
    else
      return -1;
  }    
}
hgwhittle
  • 9,316
  • 6
  • 48
  • 60
user3353475
  • 73
  • 1
  • 2
  • 5
  • 1
    Please don't post *all* your code. Just post the part that you are having trouble with. What approaches have you tried to remove the element from the array? – Vivin Paliath Mar 03 '14 at 15:53
  • `int[] array = new array[array.charAt(value) - array.charAt(value)]` will return array[0]. Is this what you wanted? – MichaelS Mar 03 '14 at 16:20
  • possible duplicate of [Removing an element from an Array (Java)](http://stackoverflow.com/questions/642897/removing-an-element-from-an-array-java) – jpdymond Mar 03 '14 at 16:22
  • `removeValue` makes no sense. Where does `i` come from? What does it mean to do `charAt` on an array? How does `array` get a value before you define it? If you're going to remove an element, isn't the size of the new array `nums.length - 1`? – Hot Licks Mar 03 '14 at 16:53

2 Answers2

0

This has been answered: Removing an element from an Array (Java)

If you want to use only arrays, view this answer: Removing an element from an Array (Java)

Community
  • 1
  • 1
jpdymond
  • 1,517
  • 1
  • 8
  • 10
-1
public static String removeValue (int[] nums)
{

int i = search(nums);

nums[i] = nums[i] - nums [i];

if(nums[i] == 0)
{
    return Arrays.toString(nums);
}
else return "invalid";
}    

This will work and instead of calling the search method in your main, call the removeValue method and pass it the array.