'Merge Sort Algorithm/Find Value

I am writing a merge sort algorithm that will sort an ArrayList then search the sorted list to see if a value match's two numbers in the list. I am getting an error after the while loop in the merge method. The error is ConcurrentModificationException but I am not too sure why. Ideally keeping the code as similar as possible.


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Value {
    static List<Integer> test = new ArrayList<>();


    static public void main(String[] args) {
        System.out.println("Generate Array");
        setUpArray(test);
        System.out.println(Arrays.asList(test));
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter Value");
        int value = scan.nextInt();
        scan.close();
        mergeSort(test);
        boolean b = findValue(test,value);
        if(b){
            System.out.println("Found");
        }else{
            System.out.println("Couldn't Find");
        }

    }


    public static void mergeSort(List<Integer> input) {
        int inputLength = input.size();
        int mid = inputLength / 2;

        if (inputLength < 2) {
            return;
        }
        List<Integer> left = input.subList(0, mid);
        List<Integer> right = input.subList(mid, inputLength);

        mergeSort(left);
        mergeSort(right);

        merge(input, left, right);
    }

    public static void merge(List<Integer> input, List<Integer> left, List<Integer> right) {
        int i = 0, j = 0, k = 0;

        while (i < left.size() && j < right.size()) {
            if (left.get(i) <= right.get(j)) {
                input.add(k, left.get(i));
                i++;
            } else {
                input.add(k, right.get(j));
                j++;
            }
            k++;
        }

        while (i < left.size()) {
            input.add(k, left.get(i));
            i++;
            k++;
        }

        while (j < right.size()) {
            input.add(k, right.get(j));
            j++;
            k++;
        }
    }


    public static boolean findValue(List<Integer> input, Integer value) {
        int i = 0;
        int j = input.size() - 1;

        while (i < j) {
            if (input.get(i) + input.get(j) == value) {
                return true;
            } else if (input.get(i) + input.get(j) < value) {
                i++;
            } else {
                j--;
            }

        }

        return false;
    }


    public static void setUpArray(List<Integer> test) {

        for (int i = 0; i < 20; i++) {
            int value = (int) (Math.random() * 100);
            Value.test.add(value);
        }

    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source