A text file contains a list of 1000 numbers that range from 1 to 100. I need to use an arrayList to store all the integers. If an integer appears multiple times in the text file, only save the first occurrence in the arrayList. Then, I need to create an output file that tells the user how many times each number occurred in the file. So far I have the following:
import java.util.ArrayList;
import java.util.Scanner;
import java.io*;
public class HW4
{
public static void main (String[] args)
{
FileReader file = new FileReader("numbers.txt");
ArrayList<int> intList = new Arraylist<int>(1000);
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext())
{
intList.add(inputFile.nextLine());
}
intList.close();
}
}
So far, I have declared the filereader and the arrayList as well as the scanner. The while loop will add the numbers from the text file to the array but I need help figuring out how to make sure each number is only stored once and the occurrences are counted. Edit: People are suggesting I use a map instead of an arrayList but my professor has required that I use an ArrayList.