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 :)