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();
		}
	}

Edited 5 Years Ago by TheSilentGiant: n/a

Starting with this: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html

and a sample that looks like this:

import java.util.*;

public class TestArrayList
{
   public static void main(String[] args)
   {
      ArrayList<Integer> arr_int1 = new ArrayList<Integer>();
      arr_int1.add(1);

      ArrayList<Integer> arr_int2 = new ArrayList<Integer>();
      arr_int2.add(50);
      arr_int2.add(75);

      ArrayList<Integer> arr_int3 = new ArrayList<Integer>();

      arr_int3.addAll(arr_int1);
      arr_int3.addAll(arr_int2);

      System.out.println(arr_int3.size() + "\n===");

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

Edited 5 Years Ago by thines01: added code

I know how to use the methods, but I am trying to figure about how to use the .add to read in the 50 integers from a file I have.

Just use the same technique you used in the first code section, but call the add()

import java.util.*;
import java.io.*;

public class TestArrayList
{
   public static void main(String[] args)
   {
      try
      {
         ArrayList<Integer> arr_int1 = new ArrayList<Integer>();
         File file = new File("C:/science/java/daniweb/396107/integers.txt");
         Scanner inputFile = new Scanner(file);

         while(inputFile.hasNext() && arr_int1.size() < 51)
         {
            arr_int1.add(inputFile.nextInt());
         }

         inputFile.close();

         for (int i=0; i<arr_int1.size(); i++)
         {
            System.out.println(arr_int1.get(i));
         }
      }
      catch(Exception exc)
      {
         System.out.println("Exception: " + exc.getMessage());
      }

   }
}
This article has been dead for over six months. Start a new discussion instead.