I have a lottery program I am trying to write for a class. Some how the data is coming in correctly threw the loop but it turns into zero's outside of the loop.
Not sure why it is doing this????
Function in the code to take in the data is below.
public static ArrayList<MegaMillions> loadMMData () {
ArrayList<MegaMillions> mmLottof = new ArrayList<MegaMillions>();
MegaMillions numbers = new MegaMillions();
String fileName; //used to get the name of the data file.
int count=0;
//setting file name of data file
fileName = "C:\\Users\\JCmobile\\Desktop\\LottoNumbersMM.txt";
try //used to handle errors
{ //preparing to import the file
File file = new File (fileName);
Scanner inFile = new Scanner (file);
do { //importing the datafile
numbers.firstNum = inFile.nextInt();
//works here
System.out.println(numbers.firstNum);
numbers.secondNum = inFile.nextInt();
numbers.thirdNum = inFile.nextInt();
numbers.fourthNum = inFile.nextInt();
numbers.fifthNum = inFile.nextInt();
numbers.megaBall = inFile.nextInt();
mmLottof.add(numbers);
//works here
System.out.println(mmLottof.get(count).firstNum);
count++;
}while (numbers.firstNum != 0);
}
catch (IOException ioe) //used to handle errors
{
System.out.println("File access error");
}
System.out.println("The number of lotto drawns are " + count);
//it prints out 0 0 0 0 0 0 0 at this point??
System.out.println(mmLottof.get(0).firstNum);
for(int i =0; i < count; i++) {
System.out.print(mmLottof.get(i).firstNum + " " + mmLottof.get(i).secondNum + " " +
mmLottof.get(i).thirdNum + " " + mmLottof.get(i).fourthNum + " " + mmLottof.get(i).fifthNum + " " +
mmLottof.get(i).megaBall + "\n");
}
return mmLottof;
}//end of loadData for mega Millions
Not sure why it is doing this????
Function in the code to take in the data is below.
public static ArrayList<MegaMillions> loadMMData () {
ArrayList<MegaMillions> mmLottof = new ArrayList<MegaMillions>();
MegaMillions numbers = new MegaMillions();
String fileName; //used to get the name of the data file.
int count=0;
//setting file name of data file
fileName = "C:\\Users\\JCmobile\\Desktop\\LottoNumbersMM.txt";
try //used to handle errors
{ //preparing to import the file
File file = new File (fileName);
Scanner inFile = new Scanner (file);
do { //importing the datafile
numbers.firstNum = inFile.nextInt();
//works here
System.out.println(numbers.firstNum);
numbers.secondNum = inFile.nextInt();
numbers.thirdNum = inFile.nextInt();
numbers.fourthNum = inFile.nextInt();
numbers.fifthNum = inFile.nextInt();
numbers.megaBall = inFile.nextInt();
mmLottof.add(numbers);
//works here
System.out.println(mmLottof.get(count).firstNum);
count++;
}while (numbers.firstNum != 0);
}
catch (IOException ioe) //used to handle errors
{
System.out.println("File access error");
}
System.out.println("The number of lotto drawns are " + count);
//it prints out 0 0 0 0 0 0 0 at this point??
System.out.println(mmLottof.get(0).firstNum);
for(int i =0; i < count; i++) {
System.out.print(mmLottof.get(i).firstNum + " " + mmLottof.get(i).secondNum + " " +
mmLottof.get(i).thirdNum + " " + mmLottof.get(i).fourthNum + " " + mmLottof.get(i).fifthNum + " " +
mmLottof.get(i).megaBall + "\n");
}
return mmLottof;
}//end of loadData for mega Millions