Java Programming Bug with ArrayList

JC724

Weaksauce
Joined
Jan 20, 2016
Messages
118
I am trying to write a program where I read in data from a file and print it to the screen. While I am trying to do A LOT more then that but I write test cases every step of the way.

I was able to pull the code in from the file. I use a function to do that.

In the same function I pass in an arraylist of a object that I am using to store the data. I test that the data is getting put into the arraylist in the function and in the main function where I call it.

I see that in the function it is working correctly but in the main function it is printing out the same line over and over again. It would seem that I copied in the last line every single time I read in a line from the file but that is not the case in the function I am using to get the data.

Not sure what is going on here?

Also what is the difference between a List and a ArrayList, I noticed I can use the inter changeably.

Code:
import java.io.*;
import java.util.*;


public class LottoCalculations {

    public static void main(String[] args)   {
        // TODO Auto-generated method stub

        // create ArrayList to store the lottoNumbers objects
        List<LottoNumbers> lPowerNumbers = new ArrayList<>();
        ArrayList<LottoNumbers> lMNumbers = new ArrayList<>();
       
       

       
         new LottoCalculations().storePowerNumbers(lPowerNumbers);
   
        //test case
        /*
        for(int i=0; i<lPowerNumbers.size();i++) {
            System.out.println(lPowerNumbers.get(i).firstNum + " " + lPowerNumbers.get(i).secondNum  + " " + lPowerNumbers.get(i).thirdNum  + " " +
        lPowerNumbers.get(i).fourthNum  + " " + lPowerNumbers.get(i).fifthNum  + " " + lPowerNumbers.get(i).megaBallpowerBall);
        }
       doesn't work */
       
//Test Case
        System.out.println(lPowerNumbers.get(0).firstNum + " " + lPowerNumbers.get(0).secondNum  + " " + lPowerNumbers.get(0).thirdNum  + " " +
                lPowerNumbers.get(0).fourthNum  + " " + lPowerNumbers.get(0).fifthNum  + " " + lPowerNumbers.get(0).megaBallpowerBall);
  //Test Case     
        System.out.println(lPowerNumbers.get(1).firstNum + " " + lPowerNumbers.get(1).secondNum  + " " + lPowerNumbers.get(1).thirdNum  + " " +
                lPowerNumbers.get(1).fourthNum  + " " + lPowerNumbers.get(1).fifthNum  + " " + lPowerNumbers.get(1).megaBallpowerBall);
      

    }//end of main
   
    public void storePowerNumbers(List<LottoNumbers> lPowerNumbers) {
       
        //CoffeeCup cup1 = new CoffeeCup();
        LottoNumbers storeValues = new LottoNumbers();
        try {
            // create a Buffered Reader object instance with a FileReader
            BufferedReader br = new BufferedReader(new FileReader("Numbers.txt"));

            // read the first line from the text file
            String fileRead = br.readLine();
            int i = 0;
           
            while(fileRead != null) {
                // use string.split to load a string array with the values from each line of
                // the file, using a comma as the delimiter
                String[] eachNumber = fileRead.split("    ");
               
                storeValues.firstNum = Integer.parseInt(eachNumber[0]);
                storeValues.secondNum = Integer.parseInt(eachNumber[1]);
                storeValues.thirdNum = Integer.parseInt(eachNumber[2]);
                storeValues.fourthNum = Integer.parseInt(eachNumber[3]);
                storeValues.fifthNum = Integer.parseInt(eachNumber[4]);
                storeValues.megaBallpowerBall = Integer.parseInt(eachNumber[5]);
               
                lPowerNumbers.add(storeValues);
               
//TEST CASE THAT WORKS
                System.out.println(lPowerNumbers.get(i).firstNum + " " + lPowerNumbers.get(i).secondNum  + " " + lPowerNumbers.get(i).thirdNum  + " " +
                        lPowerNumbers.get(i).fourthNum  + " " + lPowerNumbers.get(i).fifthNum  + " " + lPowerNumbers.get(i).megaBallpowerBall);

               
             // read next line before looping
                // if end of file reached
                fileRead = br.readLine();
                i++;
               
            }
        }//end of try statement block
     // handle exceptions
        catch (FileNotFoundException ex)
        {
            System.out.println("file not found");
        }

        catch (IOException ioe)
        {
            ioe.printStackTrace();
        }
       
       
    }//end of storePowerNumbers function
   
}//end of main class
 
Also what is the difference between a List and a ArrayList, I noticed I can use the inter changeably.

List is a more general form of ArrayList. In fact, List is specifically an interface to ArrayList, thus ArrayList must implement the same methods as List. This is why they are somewhat interchangeable.

They are not exact though. ArrayList also implements the RandomAccess interface (in Java a single class can implement many interfaces, but can only derive from (i.e. extend) only one class). So as long as you only need the methods, etc. a List interface provides, you can treat ArrayList as a List. By that same token, you can interchange an ArrayList with other classes that also implement the List interface. A good example of this is if you were to write a sorting algorithm that takes a List as argument, then it could be applied to any class that implements the List interface.
 
Re-instantiate your 'store values' object in your while loop. New object. New memory.


Code:
            while (fileRead != null) {
                // use string.split to load a string array with the values from each line of
                // the file, using a comma as the delimiter
                String[] eachNumber = fileRead.split(" ");
                storeValues = new LottoNumbers();

The gist is the array list is pointing to the 'storevalue' object's memory. Changing the object after an 'add to array' still changes what the array list is pointing to.
 
Back
Top