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.
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