• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating Objects in a loop

 
Greenhorn
Posts: 8
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did you override the toString method for your newrunner class? I'd bet not...so Java doesn't know how to print your objects.
 
Donald Bough
Greenhorn
Posts: 8
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I suggest you read this page of the Java tutorial http://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html
 
Donald Bough
Greenhorn
Posts: 8
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13089
67
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did you define the getFirstname method (and the others?) Do those methods work? How do you know?
 
Donald Bough
Greenhorn
Posts: 8
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic