aspose file tools*
The moose likes Beginning Java and the fly likes Creating Objects in a loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating Objects in a loop" Watch "Creating Objects in a loop" New topic
Author

Creating Objects in a loop

Donald Bough
Greenhorn

Joined: Dec 09, 2013
Posts: 8

I'm working on a program that will eventually allow me to change individual race results in a text file to see how it would've changed the team placing in a race. For now, I ran into a problem. First, I'm reading all the different information from a text file that is formatted as place, first name, last name, grade, school, and time. Here is what one line looks like

1 Jackson Bertoli 11 Jasper 15.29

In my code, I am taking each one of these elements, except for place because I need to generate that myself later on, and storing it in a "runner" object in order to keep each runner's information together. The problem I'm having is that I'm reading through my text file with a while loop, and I need to create a new object every time for every new line of information. I have managed to do this, but now I am stuck with many different "newrunner" objects that are all named the same and I have no clue how to distinguish between one newrunner object and another. Here's the code



So as you can see, I have a class runner, that uses runner objects with the firstname,lastname.... setup and then I am putting all of my "runner" objects in an arraylist called "amountofrunners". I am able to make the many different runner objects, but when I tried to print them out with the code



But would get something along the lines of

newrunner@32414ja
newrunner@ij123451
newrunner@25ia0d12
newrunner@1r31456

for however many runner objects I had made.
Thanks in advance for the help
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

did you override the toString method for your newrunner class? I'd bet not...so Java doesn't know how to print your objects.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Donald Bough
Greenhorn

Joined: Dec 09, 2013
Posts: 8

fred rosenberger wrote:did you override the toString method for your newrunner class? I'd bet not...so Java doesn't know how to print your objects.


I'm not familiar with overriding String methods, so that's a no. What can i do to fix this?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1950
    
  28
Can I suggest you read this page of the Java tutorial http://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html
Donald Bough
Greenhorn

Joined: Dec 09, 2013
Posts: 8

Tony Docherty wrote:Can I suggest you read this page of the Java tutorial http://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html


So the problem was with printing off my objects, not that my objects all had the same name? Instead of


I should have



And I'm sorry, but you still haven't answered how to distinguish among objects with the same names but that holding different data.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

Donald Bough wrote:I'm not familiar with overriding String methods, so that's a no. What can i do to fix this?

You don't override String methods. You override the Object class' toString method.

Remember...there is no way for Java to know what exactly you want printed out when you try to print your newly defined class. Your class gets a free "toString" method from the Object superclass, which lets it print something, but not anything terribly useful for you. You have to tell it what it should print. So, you need to create a toString method in your class file that returns a String. This will be called automatically for you when you do a S.o.p., and then it will print what you want.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

also...objects don't have names. references have names. You can have multiple references to a single object. think of a reference as holding an address, and an object as a house. on your line 20, you create a new runner object, and have newrunner refer to it. then you copy the address into your amountofrunners collection on line 21. The next time through your loop, you create a NEW runner object, and have your newrunner reference point to it. that does not change the fact that your collection has the address of the first one. you then add this address to your collection (amountofrunners).

When you then try and print out your runners, you are implicitly calling the toString method on each one. but since you haven't told java what you REALLY want to print out, it uses the default. You need to define a method like

in your runner class.
Donald Bough
Greenhorn

Joined: Dec 09, 2013
Posts: 8

OK EXCELLENT. Nobody ever explained that I'm making new references to the same object with this while loop. That makes much much more sense. I'm not familiar (bear with me) with how to build the the many different strings that I want to be printed out in my toString method though.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

Donald Bough wrote: I'm not familiar (bear with me) with how to build the the many different strings that I want to be printed out in my toString method though.


Well..the first step when writing ANY program is to forget about programming.

What do you WANT your code to print out? I don't know anything about your runner class. but tell me, in plain old English, what you want to see when you run this line:

System.out.println(amountofrunners.get(i));

Assume that particular runner has:
datafirstname = fred
datalastname = rosenberger
datagrade = A+
dataschool = Hard Knocks
dataorigtime = 12:00 noon

or feel free to use any other data set, but explain in detail what you would expect or want to see.
Donald Bough
Greenhorn

Joined: Dec 09, 2013
Posts: 8

fred rosenberger wrote:
Donald Bough wrote: I'm not familiar (bear with me) with how to build the the many different strings that I want to be printed out in my toString method though.


Well..the first step when writing ANY program is to forget about programming.

What do you WANT your code to print out? I don't know anything about your runner class. but tell me, in plain old English, what you want to see when you run this line:

System.out.println(amountofrunners.get(i));

Assume that particular runner has:
datafirstname = fred
datalastname = rosenberger
datagrade = A+
dataschool = Hard Knocks
dataorigtime = 12:00 noon

or feel free to use any other data set, but explain in detail what you would expect or want to see.


That's a really unique way of thinking about it, I like it! Whenever I print out my arraylist of runners (amountofrunners),
I want to see something along the lines of:
Jackson Bertoli 11 Jasper 15.29
Jarit Perkins 10 Jasper 15.37
Riley Stohler 12 Southridge 15.39
firstname, lastname, grade, school, time.

If I could do this, that I means I have all of my runners' information properly stored together and I can do my calculations properly later on!

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

ok...slow down.

There is a difference between printing out an arrayList, and printing out an object that is IN your arrayList. Work on one thing at a time. write some test code that creates a single runner, and prints it. Once you know you can print a single runner object, THEN figure out how to print each one in the arrayList.

The secret/most important thing to learn is to code VERY SMALL pieces at a time. I usually only write at most 3 lines of code before I re-compile and test test test test test.
Tyson Lindner
Ranch Hand

Joined: May 16, 2012
Posts: 135
Here is a short answer to print out just the first name:



Or you can create a reference within your loop and just use that.
Donald Bough
Greenhorn

Joined: Dec 09, 2013
Posts: 8

fred rosenberger wrote:ok...slow down.

There is a difference between printing out an arrayList, and printing out an object that is IN your arrayList. Work on one thing at a time. write some test code that creates a single runner, and prints it. Once you know you can print a single runner object, THEN figure out how to print each one in the arrayList.

The secret/most important thing to learn is to code VERY SMALL pieces at a time. I usually only write at most 3 lines of code before I re-compile and test test test test test.


Alright, I'm still having troubles with printing off my object references. Maybe it was with how I setup my runner class?

Then, in my while loop I just tried to print out my newrunner object right after creating it, but there's still nothing showing up in the console.
Donald Bough
Greenhorn

Joined: Dec 09, 2013
Posts: 8

fred rosenberger wrote:ok...slow down.

There is a difference between printing out an arrayList, and printing out an object that is IN your arrayList. Work on one thing at a time. write some test code that creates a single runner, and prints it. Once you know you can print a single runner object, THEN figure out how to print each one in the arrayList.

The secret/most important thing to learn is to code VERY SMALL pieces at a time. I usually only write at most 3 lines of code before I re-compile and test test test test test.


Also, I tried printing out my datafirstname, datalastname... directly in the while loop with a system out and NOTHING PRINTED. Maybe there is a bigger problem?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

did you define the getFirstname method (and the others?) Do those methods work? How do you know?
Donald Bough
Greenhorn

Joined: Dec 09, 2013
Posts: 8

fred rosenberger wrote:did you define the getFirstname method (and the others?) Do those methods work? How do you know?


I had one of my friends who is a computer science student check it out (I'm just a Junior in highschool) and he figured out that I was actually using my scanner that was reading the text file in a method before readfile, so it couldn'tread the text file again with the while loop. Also, he put everything in a list, then after the while loop, he put that list into an array. Thank you for all the help that you have given me on this project so far. I've found this forum to be the most responsive of the different Java forums, thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating Objects in a loop
 
Similar Threads
scope problem
URLConnection and Applets...
Searching for a better way of stripping a file
creating arrays from data in a text file
General uneasy feeling about java logging...... or maybe it's just too much eggnog