Initially I was to design this simple program to have a file be read in and store 50 numbers in array1, and the last 50 numbers in array2, then calculate a total (array1 + array2) and store it in array3. But I need to change this program to utilize the ArrayList class, which I am a bit confused on how to use that in order to have a file read in to store numbers in the array3.
import java.io.*;
import java.util.Scanner;
public class Lab {
public static void main(String[] args)throws IOException {
int[] array1 = new int[50];
int[] array2 = new int[50];
int[] array3 = new int[50];
int index1 = 0, index2 = 0;
File file = new File("C:\\Users\\User\\Desktop\\inputNumbers.txt");
Scanner inputFile = new Scanner(file);
//reading values into array1
while(inputFile.hasNext()&& index1 < array1.length){
array1[index1] = inputFile.nextInt();
index1++;}
//reading values into array2
while(inputFile.hasNext()&& index2 < array2.length){
array2[index2] = inputFile.nextInt();
index2++;}
//getting sum of first two arrays
for(int index = 0; index < 50; index++){
array3[index] = array1[index] + array2[index];
System.out.println(array1[index]+ "\t"
+ array2[index] + "\t" + array3[index]);}
inputFile.close();
}
}