ArrayList to take in a datafile not working??

JC0133

Senior member
Nov 2, 2010
201
1
76
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.

Code:
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
 
Last edited by a moderator:

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,661
4,603
75
Use code tags. :colbert:

I see. You're add()ing numbers to the ArrayList. But remember, you're not adding the values in numbers to the ArrayList. You're adding a reference to numbers to the ArrayList.

Does this give you any new ideas? :hmm:
 

Net

Golden Member
Aug 30, 2003
1,592
3
81
what ken g6 said should help you figure it out.

once you understand that you can clean up the code by doing the following.

instead of doing numbers.firstNum, numbers.secondNum, etc.... just add the numbers to an arraylist. so make MegaMillions have a member variable: private List<Integer> numbers = new ArrayList<Integer>();

and a method that returns the list, .getNumbers().

then you cand do: MegaMillions.getNumbers().add(inFile.nextInt())

then also make a method to set the megaball number, MegaMillions.setMegaBall(inFile.nextInt())

also instead of a do loop, do a for loop and end when there inFile.hasNext() is false.

Also read up on static method and classes to know when to use them. Here are some good rules of thumb when to use them, utility classes i.e. Math.round(4.3), Doesn't make sense to make an instance of the class...
Don't return an ArrayList, remember you want to program to interfaces not implementations. So return a general List. That way who ever uses your api can pass it into their list types without having to loop through the entire thing to cast it to arraylist. Take a moment and look at the methods that arraylist has that list doesn't have. This will be good learning for you, then ask yourself "does everyone that uses this MUST use arraylists specific methods" most always the answer is no.
 
Last edited:

JC0133

Senior member
Nov 2, 2010
201
1
76
Thank you I got it to work. So a few more questions

How do I put my code in the proper format for the future on here?

Can you pass values by reference or is it just a copy(value) in Java?

If I was to create an instance of a class in main and then pass it to a function would that be pass by reference?

Ex.

main(...) {
MegaMillions lotto = new MegaMillions();

loadMMdata(lotto);
}

public static void loadMMdata(MegaMillions fLotto) {


//what ever I do to fLotto in this function will it reflect on lotto in main?

}

Also my last question is why was my original code pass by value? I thought pass by value or reference was only valid when you were dealing with pass parameters into functions?
 

Net

Golden Member
Aug 30, 2003
1,592
3
81
Thank you I got it to work. So a few more questions

How do I put my code in the proper format for the future on here?

Can you pass values by reference or is it just a copy(value) in Java?

If I was to create an instance of a class in main and then pass it to a function would that be pass by reference?

Ex.

main(...) {
MegaMillions lotto = new MegaMillions();

loadMMdata(lotto);
}

public static void loadMMdata(MegaMillions fLotto) {


//what ever I do to fLotto in this function will it reflect on lotto in main?

}

Also my last question is why was my original code pass by value? I thought pass by value or reference was only valid when you were dealing with pass parameters into functions?

http://www.javaworld.com/javaqa/2000-05/03-qa-0526-pass.html
 

BigDH01

Golden Member
Jul 8, 2005
1,631
88
91
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.

Code:
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

Also, you should put a little more thought into your comments. I think comments should be used to describe abstract behavior, not something that is obvious from your code. I just noticed this in a code review today, someone was adding comments telling people that name of the property which they were getting/setting. I don't need a comment to tell me the name of a property, I can just look at the property. Same with your comment about "//used to handle errors." Any programmer is already going to know why you have a try/catch block there, what they would want to know is if there is any special about this particular one. Should I expect that error to be thrown in unusual circumstances?

Same with your "setting the name of the data file." A programmer can see you are setting a value there, the comment isn't useful. Is there anything I should know about the file you're loading there? Does it have to be in a certain folder? Does it have to adhere to a specific format? Tell me something useful that can be used to save time later.
 

cytg111

Lifer
Mar 17, 2008
25,894
15,352
136
...Also my last question is why was my original code pass by value? I thought pass by value or reference was only valid when you were dealing with pass parameters into functions?

everything in java is by reference ... which, the other way around, always screws me over on my first c function when going back.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,661
4,603
75
Actually, I like to think of Java as a "pass reference by value" language. Every argument is actually passed by value - the value of any variable being passed in is not changed. However, if you pass an Object - as opposed to any of the primitive types like int - then you're passing a reference by value. So any changes to the Object the reference refers to will be seen by other variables with the same reference. But if you pass in an Object and then reassign the variable in the method to another Object, the original Object is unchanged.

Confused yet? ;)
 

JC0133

Senior member
Nov 2, 2010
201
1
76
I am still a little confused. I apologize if these are dumb questions. I am trying to understand the differences of pass reference by value and pass by reference ??

I understand pass by value and pass by reference from C++ but from my understanding pass reference by value is like passing a pointer correct?? The pointer points to the address and not the value right? So isn't pass reference by value and pass by reference the same thing??
 

JC0133

Senior member
Nov 2, 2010
201
1
76
Also, you should put a little more thought into your comments. I think comments should be used to describe abstract behavior, not something that is obvious from your code. I just noticed this in a code review today, someone was adding comments telling people that name of the property which they were getting/setting. I don't need a comment to tell me the name of a property, I can just look at the property. Same with your comment about "//used to handle errors." Any programmer is already going to know why you have a try/catch block there, what they would want to know is if there is any special about this particular one. Should I expect that error to be thrown in unusual circumstances?

Same with your "setting the name of the data file." A programmer can see you are setting a value there, the comment isn't useful. Is there anything I should know about the file you're loading there? Does it have to be in a certain folder? Does it have to adhere to a specific format? Tell me something useful that can be used to save time later.


I will try to be better with my comments. Thank you.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
I am still a little confused. I apologize if these are dumb questions. I am trying to understand the differences of pass reference by value and pass by reference ??

Yeah, it's confusing, but understanding it is essential. For the moment, think of a reference as a numerical address identifying a specific location in memory. It basically is that in C++, but other languages treat them more abstractly. So let's say you have an object in memory. A reference to that object (or a pointer to that object) is a number, just like an int is a number, and a long is a number (in fact it should be the same size as a long). If you "pass a reference" to that object to some method, you are passing the numeric address. The address itself is pushed onto the stack and passed by value, like any other number. That's all that "pass reference by value" means. It's to help you remember that when you pass any scalar value, including a reference or pointer, to a method it is copied onto the stack.