1

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.

Adam Shick
  • 11
  • 2
  • 1
    Use ArrayList.contains() to figure out if an integer is in the list, and Integer.parseInt() to convert lines into integers. Refer to this question for info on reading from files: https://stackoverflow.com/questions/4716503/best-way-to-read-a-text-file-in-java – Matt Eskridge Nov 12 '15 at 00:35
  • 2
    What have you tried? This isn't a place to dump your projects and let the community solve it. – Wyatt Lowery Nov 12 '15 at 00:35
  • 2
    What have you tried so far and where are you stuck? You can't expect the community to solve the problem for you – Michael Nov 12 '15 at 00:35
  • Please post that in your actual question and format it correctly – Michael Nov 12 '15 at 00:37
  • What are your exact requirements? Do you _have_ to use an ArrayList, or can it be something more appropriate, such as a Map? – Klitos Kyriacou Nov 12 '15 at 00:49
  • @KlitosKyriacou I am required to use an ArrayList to store the integers – Adam Shick Nov 12 '15 at 00:54

1 Answers1

3

A couple things to note

  1. Your ArrayList cannot be declared to hold an int. It can only work with objects, not primitives
  2. Scanner#nextLine returns a String, not an Integer. You should instead use Scanner#nextInt. Or, you could cast your String to an Integer by calling Integer#valueOf
  3. Your use of an ArrayList is questionable at this point. I suggest using a Map instead. It is...

    An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value

Thus, your code can then look like this(the relationship is number, # o/occurrences):

Map<Integer, Integer> myMap = new HashMap<Integer, Integer>();
while (inputFile.hasNext()){
    Integer next = inputFile.nextInt();
    if (myMap.containsKey(next)){
        myMap.put(next, myMap.get(next) + 1);
    }else{
        myMap.put(next, 1);
    }
}
Michael
  • 2,673
  • 1
  • 16
  • 27
  • plus 1 for the map suggestion. this would have been my go-to answer as well. – Iofacture Nov 12 '15 at 00:54
  • unfortunately I am required to use an arrayList for this assignment, and cannot use a map. Thank you very much for your feedback though. I appreciate it! – Adam Shick Nov 12 '15 at 00:55
  • You could still use a Map to store the occurrences of each number, while also adding the number to your ArrayList. But you might also consider using just a simple array of 101 ints holding the occurrences, which could be more space- and time- efficient than a Map. – Klitos Kyriacou Nov 12 '15 at 01:02
  • piggybacking off of this answer you can use autoboxing to achieve your requirement of using an arrayList – Michael Queue Nov 12 '15 at 01:24
  • Would it be considered cheating if you used the map, exactly as in Michael's answer, and then at the end you did `ArrayList intList = new ArrayList<>(myMap.keySet());` ? – Klitos Kyriacou Nov 12 '15 at 23:29