2

When I try and find+delete a value in my ArrayList, that is over two digits, my method returns -1 (The value if not found). However it works fine for values below 100. To make it work you type in the depth(index) of the value in the ArrayList, and the value you wish to find. When you find both of them it deletes the value from the array. For some reason the if statement does not work when the value is three or more digits.

Here is my code:

import java.util.*;

public class EP {

    public static List < Integer > items = new ArrayList < Integer > (Arrays.asList(12, 13, 48, 42, 38, 2827, 827, 828, 420));

    public static void main(String[] args) {
        moreLines();

        System.out.println("Exam List");

        for (Integer i: items) {
            System.out.println(i);
        }

        moreLines();

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter depth");

        int depth = scan.nextInt();
        moreLines();

        System.out.println("Enter value");
        int value = scan.nextInt();

        moreLines();

        System.out.println(mark(depth, value));
    }

    public static int mark(int depth, int value) {
        int ret = -1; //This ensures -1 is returned if it cannot find it at the specified place

        for (int i = 0; i < items.size(); i++) {
            System.out.println(items.get(i));

            /** This is the condition for the value deletion
             *  If depth and value are correct, it deletes the value
             */
            if (items.get(depth) == (Integer) value) {
                ret = value;
                items.remove(items.get(depth)); //removes ArrayList Item
            }
        }

        moreLines();

        System.out.println("Updated Exam List"); //Shows ArrayList with deleted item

        for (Integer j: items) {
            System.out.println(j);
        }

        moreLines();

        return ret;
    }

    public static void moreLines() { //Just there for debugging, makes spaces when you run it. 
        System.out.println("   ");
        System.out.println("   ");
    }
}

Please excuse the poor commenting, and the layout. It is hard to get it properly in StackExchange, Copy and Pasting does not work well, So I had to manually space it. Thanks :)

Mufaka
  • 2,333
  • 1
  • 18
  • 25

2 Answers2

2

Problem:

if(items.get(depth) == (Integer)value)

Value should not be cast to an integer.

Solution:

if(items.get(depth) == value)

Simply take out the cast and the conditional will do the rest.

DarkV1
  • 1,776
  • 12
  • 17
0

I think the problem is not with the number of digits but the fact that all your 3 digit numbers are in the end but to do with the fact that you are removing items from the list which changes the depth (index) of the remaining items from what it used to be in original list. It's not a good idea to remove an item if you are iterating over it.

Vishal
  • 549
  • 3
  • 13
  • the problem is that he tried to cast an int to an integer and compare it with an "Integer" array... But, an int would work. – DarkV1 May 05 '16 at 04:22
  • Follow the link that the moderator assigned to the page. It explains more in depth to the problem – DarkV1 May 05 '16 at 04:22