Results 1 to 9 of 9
- 01-09-2012, 09:47 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 8
- Rep Power
- 0
Question dealing with a simple object (hard to explain)
So basically, I have a person object with the instance variable "name" which is a string. You can get and set the name of a person. In my program (which uses swing) I have a panel where you can use textfields to type in a name. It will get the text from that textfield, and set the name of a a person I created in my code called p1 (person p1 = new person...). On another panel I display the names of my people with a for each loop (since I am adding my persons to an arraylist) and all of this works fine. The problem I have is that since when you register, the name of p1 is being set to the text you register with, there can only be ONE person since the name of the same person (p1) is just being changed each time you register. How would I make it so that if you typed in a name, it would create a NEW patron and set the name of that, instead of simply changing the name of p1. As far as I know, its impossible to create a new person outside of the code, so I am having a lot of trouble. Any help would be appreciated, thanks!
- 01-09-2012, 10:06 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Question dealing with a simple object (hard to explain)
The following is a bit of a guess...
If you are adding the people to a list (or other collection) you must create a new person add add him/her to the list. You cannot alter the names of people and add them numerous times to the list. That will leave you with a collection containing a single person numerous times (the name of this person being the last thing you set it to.
-----Java Code:import java.util.ArrayList; import java.util.List; public class Person { private String name; public Person(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return name; } @Override public String toString() { return name; } public static void main(String[] args) { List<Person> list = new ArrayList<Person>(); // this is no good Person person = new Person(""); person.setName("Tom"); list.add(person); person.setName("Dick"); list.add(person); person.setName("Harry"); list.add(person); System.out.println(list); // do it this way list = new ArrayList<Person>(); person = new Person(""); person.setName("Tom"); list.add(person); // or just person = new Person("Dick"); list.add(person); // or even list.add(new Person("Harry")); System.out.println(list); } }
If your problem is something else, describe it with runnable code and a description of both observed behaviour and your intended or desired behaviour. The code should not include things that are not relevant to the problem.
- 01-09-2012, 10:48 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 8
- Rep Power
- 0
Re: Question dealing with a simple object (hard to explain)
Thanks for the reply! But yeah, what I am trying to do is a bit different. Posting my code would be useless also since everything relevant is... well... a lot, and I have no idea what to do so I haven't really tried anything. Let me just try to explain myself better (if possible). I know how to do what you just said, but you see, I also want to make it so that the user of my program can create their own person and give their person a name of their choice. The problem with this is that I have no idea how to let the user of my program somehow create new persons without me physically typing "person p1 = new person.." in my own code. All I can currently let the user do is set the name of an already existing person I have made in the code, but I cant let them make their own person. I hope that was a bit clearer, sorry
- 01-09-2012, 10:57 PM #4
Re: Question dealing with a simple object (hard to explain)
I know what you're saying. You just need to do what pbrockway2 is saying and make the person creation and referencing anonymous. You cannot name every single person in the system with a variable, what if you had a billion people? You thing in such systems they code in a billion p1, p2, p3, ... p1000000000? Nope!
- 01-09-2012, 11:16 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Question dealing with a simple object (hard to explain)
No the point is not to post your code. But to (think about, write and) post code that is illustrative of the problem.Posting my code would be useless also since everything relevant is... well... a lot
Again, it's the *problem* that needs description. (The business about only one person being possible.)and I have no idea what to do so I haven't really tried anything.
From the programmer's point of view this is not what happens. The code creates the person (using the new keyword). It may use a string supplied by entered by the user (including in a GUI component), and the person creation process may be triggered by some user action (including a button click or other gui action). All this gives the user the illusion that they created a person.I also want to make it so that the user of my program can create their own person and give their person a name of their choice.
But it is an illusion. The programmer does not have to stand ready to add new code to deal with whatever it is the user wants to do (and specifically to create a person with the particular name they might have thought up). Rather the code is already there, and the user is constrained to follow paths established in advance. They aren't aware of the constraints, but that's part of the illusion.
So simple code whereby "the user ... can create their own person and give their person a name of their choice" might look like:
In a gui the role of (1) might be played by a button or the enter key pressed in the text field, (2) could consist of reading the contents of a text field. The critical part, (3), will be present whatever form (1) and (2) take. (4) stands in for "stuff the program might do later with the newly created people".Java Code:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class Person { private String name; public Person(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return name; } @Override public String toString() { return name; } public static void main(String[] args) throws IOException { List<Person> list = new ArrayList<Person>(); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Would you like to create a person?"); // (1) String resp = in.readLine(); while(resp.toLowerCase().startsWith("y")) { System.out.println("Would you like the person to be called?"); // (2) list.add(new Person(in.readLine())); // (3) System.out.println("Would you like to create another person?"); // (1) resp = in.readLine(); } System.out.println(list); // (4) } }Last edited by pbrockway2; 01-09-2012 at 11:20 PM.
- 01-10-2012, 01:22 AM #6
Member
- Join Date
- Dec 2011
- Posts
- 8
- Rep Power
- 0
Re: Question dealing with a simple object (hard to explain)
Ah ok, I understand what youre saying, ill try this out :)
EDIT: It completely WORKED! And it was so simple too XD
Thanks so much!!
DOUBLE EDIT: One more question actually... hehe... so it works now, but since im not specifying a name for my person, how do I get something from it? What I mean is that I am setting one of the instance variables of my person (not the name) to a random number using a random number generator, and want to know how to retrieve this from my person which I do not know how to refer to. Before what I did was this:
But I dont know what to substitute for "person" anymore. Any help on this would be appreciated as well. Sorry for all the troubleJava Code:Blah.setText(person.getNumber());
Last edited by Lanuk; 01-10-2012 at 01:56 AM.
- 01-10-2012, 02:27 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Question dealing with a simple object (hard to explain)
It's no trouble. But a lot depends on circumstances.
If you are displaying everybody's name and number you would use a simple for loop:
On the other hand if you want the number of a particular person you have to have a clear idea of which person you mean. For example if it was the last person to be added you might say:Java Code:for(Person person :list) { // or whatever you want to do with the number System.out.printf("Name: %s Number: %d%n", person.getName(), person.getNumber()); }
Or it could be that you have the people displayed in a JList and you want to display the number when a selection is made. In that case you get the selection from the (j)list which will be a Person instance (or can be cast to one). Then just call getNumber() on that object and use it.Java Code:Person toAdd = new Person(/*name from user*/); list.add(toAdd); // display their number in a text field numberTF.setText(toAdd.getNumber);
Or it could be that you want the number of a person with a particular name. Again, a for loop:
Or... I really does depend which person you are trying to get the details for. (It may even turn out to be the case that an ArrayList is not the most appropriate data structure.)Java Code:String toFind = // get name from user, eg from text field id = -1; for(Person person :list) { if(person.getName().equals(toFind)) { id = person.getNumber(); break; } } // again, do whatever you like with the result if(id < 0) { System.out.println("Not found!"); } else { System.out.println("Number is " + id) }
- 01-10-2012, 04:15 AM #8
Member
- Join Date
- Dec 2011
- Posts
- 8
- Rep Power
- 0
Re: Question dealing with a simple object (hard to explain)
Oh ok, that helps a lot, thanks!
- 01-10-2012, 04:40 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
a hard question about process
By birnie2525 in forum Threads and SynchronizationReplies: 2Last Post: 03-22-2011, 01:00 PM -
Can it be that hard to make simple chat?
By Ruuhkis in forum NetworkingReplies: 2Last Post: 02-25-2011, 05:13 PM -
hard question .. help me .. about creating random numbers
By soldier in forum New To JavaReplies: 5Last Post: 12-21-2009, 01:17 PM -
Dealing with exceptions in my simple GUI app involving a process
By fawkes711 in forum AWT / SwingReplies: 2Last Post: 12-08-2009, 08:33 PM -
Explain how to troubleshoot simple java pg
By senthil in forum Advanced JavaReplies: 2Last Post: 09-01-2009, 01:28 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks