• Post Reply Bookmark Topic Watch Topic
  • New Topic

creating an object from user input  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!!!

if i have a class called manager that takes in user input and asks the user to enter a team name,and i want the team name to be a Team Class object in an array list
where is the method to create a team best kept?

in the Team Class? or in a method in a different class?

is it ok to instantiate an object from a Class in a method of the same Class?

all im trying is

user input - team name - make object in arraylist of team name.

i havent done any code yet...as im trying to work out which is the best way to go about this

thanks for reading
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it ok to instantiate an object from a Class in a method of the same Class?


This is usually a sign of very bad design. Why would you ever want to do such a thing?
Your Team class might include something like this:
So you see the teamName is just an instance variable of the Team - no need for an object of type Team inside the Team class, since it is already a Team.
And if you were to try to create a Team instance inside the Team constructor, you would end up with an infinite recursion.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Fred,

this is what i did just before coming back here:



but still,how can i get objects created via user input
so it asks the user:
enter team name:
Bayern Munich

then creates a Bayern Munich (My Champions League Team) object that can be referenced in an arraylist

thanks for answering
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so i have it working this way...though im sure its untidy and cant see how im going to add this to an arraylist in the method


 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is this list of Team objects supposed to represent? Is it a list of teams that belong to a league? Teams playing in a tournament? Seems like you'd also need to know what kind of object would manage this list of teams.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes junilu,

a league

im just taking it one step at a time...
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You create a new object by using the 'new' keyword. You've already done this.
I think you should tweak the constructor so that it takes a 'teamName' as a parameter. Then you'd have
And then adding it to a List is as easy as
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes carey...i have done all this...

but what i was wondering was that should it be done in a method or just in my main method?

thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!