ArrayList to take in a datafile not working??

JC0724

Weaksauce
Joined
Oct 11, 2008
Messages
105
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
 
I'm not a Java dev, but after digging some through their docs.... My guess is that you need to cast the returned object from the "ArrayList.get()" function to a type "MegaMillions".

Also, please wrap your code sample in the "CODE" tags to improve readability.

Edit: Scrap that, I mis-read the Java docs.
 
Last edited:
My guess is that mmLottof.add(numbers) is not actually copying the contents like you would expect, but instead adding a reference to "numbers" which is getting overwritten each time through the loop (with 0's on the last iteration).

I recall Java using pass-by-reference for anything other than basic types.
 
Last edited:
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?
 
How do I put my code in the proper format for the future on here?
Wrap it using the "CODE" tags. Easiest way to find it is from the "Advanced" post options, and hover over the hash-button in the text editor.


Can you pass values by reference or is it just a copy(value) in Java?
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?
Java is pass by value, but with exception for arrays. Though I'll defer to some regular Java devs to elaborate or clarify as needed.
 
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?

Java is pass-by-value. The object you create in main is assigned to a REFERENCE variable called lotto. This reference is then passed to a method by value. The object POINTED to by the reference however, can be changed in the method. Confused?

This is true pass-by-reference, which java DOES NOT support. Apologies in advance for my terrible c++:
Code:
void test(int& a) {
   a = 5;
}

int main() {
   int a = 0;
   test(a);
   printf("%d", a); //prints 5

   return 0;
}

In java though, it's perfectly acceptable to manipulate an object in a method:

Code:
private void test(SomeBean bean) {
   bean.setValue("value");
}

public static void main(String[] args) {
   SomeBean bean = new SomeBean();
   test(bean);
   System.out.println(bean.getValue()); // will print "value"
}

Assuming you already have a class called SomeBean which encapsulates the value string:

Code:
class SomeBean {

public void setValue(String val) {
   this.value = val;
}

public String getValue() {
   return value;
}

String value;
}
 
Wrap it using the "CODE" tags. Easiest way to find it is from the "Advanced" post options, and hover over the hash-button in the text editor.




Java is pass by value, but with exception for arrays. Though I'll defer to some regular Java devs to elaborate or clarify as needed.

Arrays in Java are also technically pass-by-value. Basically you are just passing a pointer.
 
Arrays in Java are also technically pass-by-value. Basically you are just passing a pointer.
Yes, but modifications to items in the array are preserved... correct?
 
Yes, but modifications to items in the array are preserved... correct?

Correct. The array isn't passed by value, a reference to the array is passed by value.

Java is pass-by-value as others have indicated. All arguments in Java are references. So really you could just say Java is 'references-passed-by-value'.

The only thing that does not stick when you change a passed in argument is if you attempt to reassign that argument as the reference has been copied. Changing the reference in the method will not change it for the caller. Any changes to the state of the object being referred to will be seen by the caller.
 
Last edited:
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??
 
Example time!

Using khaki's SomeBean class from above

Code:
class SomeBean {

public void setValue(String val) {
   this.value = val;
}

public String getValue() {
   return value;
}

String value;
}

Code:
public static void main(String argv[]){
  SomeBean x = new SomeBean();
  x.setValue("mainValue");
  reassignX(x);
  System.out.println(x.getValue()); //prints 'mainValue' as the caller doesn't see the reassignment
  mutateX(x);
  System.out.println(x.getValue()); //prints 'changedMethodValue', previous method changed the object not the reference

}

/* This doesn't work because the reference for the argument is copied*/
public static void reassignX(SomeBean x){
  x = new SomeBean();
  x.setValue("newMethodValue");
}

/* This works because we haven't tried to change the reference we just changed the referred objects state. */
public static void mutateX(SomeBean x){
  x.setValue("changedMethodValue");
}
 
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??

Not quite. As seen in Devman's example, in pass by value, the object pointed to by the reference can not be changed by the callee in a way that is seen by the calling method. In c++, with pass by reference, this is indeed possible. Again, I do not regularly program in c++, apologies!

Code:
#include <vector>

void callee(std::vector<int>& vec) {
	std::vector<int> v;
	v.push_back(65);

	vec = v;
}

int main(int argc, char* argv[])
{
	std::vector<int> v;
        std::vector<int> *vPtr = &v;
	v.push_back(5);      
        
	callee(v);

	printf("%d\n", v[0]); // prints 65!
        printf("%d\n", vPtr->at(0)); // also prints 65!

	return 0;
}

Furthermore, passing a reference by value is not as powerful as passing a raw pointer. Pass by reference-like behavior can be achieved by passing raw pointers around in a language that allows them. Not so with a reference passed by value in Java.
 
Back
Top