1

In my program I am first searching for a value, if it exists the array position is given, if not -1 is given but when i try to return the error message in my program that the value entered could not be found therefore not deleted I receive an actual error. This may sound confusing and I can try to explain more if you do not see what I am saying by looking at my code. Can someone help?

private int search(int s){
    int position = 0;
    while(nums[position] != s){
        if(position < size)
            position++;
        else
            position = -1;
    }
    return position;
}
public void delete(int d){
    int value = search(d);
    if(search(d) == -1){
        System.out.println("The value " + d + " was not found and cannot be deleted");
    }
    else

    for(int n = size; n > value; n--)
        nums[value] = nums[size];
}

I receive the error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Numbers.search(Numbers.java:44)
at Numbers.delete(Numbers.java:53)
at Assignment7.main(Assignment7.java:31)
user3363245
  • 31
  • 1
  • 6

4 Answers4

0

I don't know Java, but I know C#. I think search method searches for existing entries.

Don't use ifcondition.

Catch the exception in the search code and the in the catch block, do this:

System.out.println("The value " + d + " was not found and cannot be deleted");
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
0

Make sure that variable size is equal to the array length nums.length-1;

int size=nums.length-1;
Nambi
  • 11,944
  • 3
  • 37
  • 49
0

Your information are not enough to trace the code completely but this line:

for(int n = size; n > value; n--)
    nums[value] = nums[size];

seems very suspicious.

Edit according to OP's comment:

while(nums[position] != s){
    if(position < size)
        position++;

ok, simply you increase the position up to size, that is out of bound.(Seems size equals nums.lenght)

BTW I suggest you to use ArrayList like this, cause it provides a lot of functionality for you:

ArrayList<Integer> nums = new ArrayList<Integer>();
//....
return nums.indexOf(s); //s is an Integer
Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
  • I actually just plugged that in randomly and it worked, I haven't tried to figure out the logic behind it yet – user3363245 Apr 18 '14 at 05:52
  • is my last statement true?" size equals nums.lenght" – Mohsen Kamrani Apr 18 '14 at 05:53
  • actually size is a separate int that keeps track of how many int's have been added to the array, so if 4 int's were added to an array with length 10 then size would be 4 – user3363245 Apr 18 '14 at 05:55
  • To trace better, print `nums.length` and `size` before the `while` and print `position` in the first line of the `while`, I think you will find the problem easily this way, please report the result if it was not clear for you. – Mohsen Kamrani Apr 18 '14 at 05:58
0

Edit3:

Here I have added better code than my last edit.

This has removed the array element. Normally I wouldn't do work for you but this is hard code to work out without a bit of technical knowledge, at least in my mind, unless you want to keep coding and fixing.

Your search worked, but the remove implementation didn't. As mentioned before, your for loop was not right. It kept repeating the same instruction (placing the searched value at the end of the array, deleting the before value forever) and ended up with you not deleting anything important and even losing information, if you had wanted to keep it.

The code below should help you in figuring out how to complete the intended functions for your program.

public class SearchTest {

static int nums[] = new int[] { 1, 2, 3, 4, 5 };

public static void main(String[] args) {

    delete(2);
    //deletes the number 2, if found

    for (int i = 0; i < nums.length; i++) {

        System.out.println(nums[i]);

    }

}

public static void delete(int d) {
    int value = search(d);
    if (value == -1) {
        System.out.println("The value " + d
                + " was not found and cannot be deleted");
    } else
        //removes the element, since it was found
        nums = removeElement(nums, value);
}
/**
*http://stackoverflow.com/questions/4870188/delete-item-from-array-and-shrink-array
* This is the remove method from this post, look at this link for more
**/

public static int[] removeElement(int[] original, int index) {
    int[] n = new int[original.length - 1];
    System.arraycopy(original, 0, n, 0, index);
    System.arraycopy(original, index + 1, n, index, original.length - index
            - 1);
    return n;
}

private static int search(int s) {
    int position = 0;
    while (nums[position] != s) {
// changed to nums.length
        if (position < nums.length)
            position++;
        else
            position = -1;
    }
    return position;
}
}
BoydyBoydy
  • 159
  • 1
  • 9
  • Still checking the Delete method, there seems to be alot wrong with it. – BoydyBoydy Apr 18 '14 at 06:27
  • Like it was suggested before you should use something like an ArrayList, as Mok has stated. Also if size means the number of elements in an array you should replace my code with size and not nums.length, if you use it. – BoydyBoydy Apr 18 '14 at 06:55