0

I'm trying to write a simple program using an ArrayList that adds values, removes values, and prints values. However, I'm having an issue with removing values that the user inputs. The user should input a designated amount of values (determined by the user) and then the program should remove those values (if there are duplicates then it should just remove the first instance of the number which is fine). It's not removing the values and in some cases removes one value. I'm a bit confused as to where my code is bugged. Here is my code for remove values:

private static void removeVals() {
        System.out.println("How many values would you like to remove?");
        int amountToRemove = scanner.nextInt();
        scanner.nextLine();
        System.out.println("Enter " + amountToRemove + " values:");
        for(int i = 0; i < amountToRemove; i++) {
            int vals = scanner.nextInt();
            if(arrayList.get(i).equals(vals)) {
                arrayList.remove(i);
            }
        }
        System.out.println("Values removed");
    }

And here is my full code:

public class Main {

    private static ArrayList<Integer> arrayList = new ArrayList<Integer>();
    private static Scanner scanner = new Scanner(System.in);

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

        int option = scanner.nextInt();
        while (option != 0) {
            switch(option) {
                case 1:
                    addVals();
                    printOptions();
                    option = scanner.nextInt();
                    break;
                case 2:
                    removeVals();
                    printOptions();
                    option = scanner.nextInt();
                    break;
                case 3:
                    printVals();
                    printOptions();
                    option = scanner.nextInt();
                    break;
                case 4:
                    printOptions();
                    printOptions();
                    option = scanner.nextInt();
                    break;
                default:
                    System.out.println("Not a valid option, please enter again:");
                    option = scanner.nextInt();
                    break;
            }
        }
    }

    private static void addVals() {
        System.out.println("How many values would you like to add?");
        int amountToAdd = scanner.nextInt();
        scanner.nextLine();
        System.out.println("Enter " + amountToAdd + " values:");
        for(int i = 0; i < amountToAdd; i++) {
            int vals = scanner.nextInt();
            arrayList.add(vals);
        }
    }

    private static void removeVals() {
        System.out.println("How many values would you like to remove?");
        int amountToRemove = scanner.nextInt();
        scanner.nextLine();
        System.out.println("Enter " + amountToRemove + " values:");
        for(int i = 0; i < amountToRemove; i++) {
            int vals = scanner.nextInt();
            if(arrayList.get(i).equals(vals)) {
                arrayList.remove(i);
            }
        }
        System.out.println("Values removed");
    }

    private static void printVals() {
        for(int i = 0; i < arrayList.size(); i++) {
            System.out.print(arrayList.get(i) + " ");
        }
        System.out.println();
    }

    private static void printOptions() {
        System.out.println();
        System.out.println("Program options: ");
        System.out.println("1. Add values\n2. Remove values\n3. Print values\n4. Print options\n0. Exit\n");
        System.out.println("\nWhat would you like to do? (enter an option):");
    }
}
Josh
  • 55
  • 3
  • 10

1 Answers1

1

arrayList.remove(i) removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). that is the bug your code for more details refer below link https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#remove(int) ,

bakki
  • 314
  • 3
  • 10