1

So right now I'm writing a program that will take a user input such as:

  • "Jeff" for a first name
  • "Henderson" for a last name
  • a phone number with them
  • an amount to be stored in their bank account

And store it all into a Linked List.

If I were to add another person with this similar info, how would that be stored in the link list such at I could access both individuals information separately?

Right now I'm using this to establish all the storage when entering a new user:

private final Scanner keyboard;

private final LinkedList<String> firstName;
private final LinkedList<String> lastName;
private final LinkedList<String> teleNumber;
private final LinkedList<String> accountBalance;

public bankdata() 
{
    this.keyboard = new Scanner(System.in);
    this.firstName = new LinkedList<String>();
    this.lastName = new LinkedList<String>();
    this.teleNumber = new LinkedList<String>();
    this.accountBalance = new LinkedList<String>();
}

I plan to use keyboard.next(); to read the user input and then use an add method I've written to store the information into each linked list.

The way it is set up is that there is a Linked List for every single object variable like:

  • a Linked List for first name
  • a Linked List for the last
  • a Linked List for the tele number
  • a Linked List for the account balance

If I want to call information associated with one specific person, how do I have it so when I request that users information that I can see all his stuff if they're in another linked list?

Ryan Tibbetts
  • 412
  • 1
  • 8
  • 22

3 Answers3

2

The thing here is to create your own data structure. I'd say research a bit more about LinkedList. Almost every linked list example online can help you out.

Hint: Create a class for your node, and store all information pertaining to each person in that node.

EDIT: Just to complete the answer: How do I create a Linked List Data Structure in Java?

EDIT 2: Based on your edited OP, I think you want to have 4 LinkedLists, and find all information for one person. I'll assume you are going to search by the person's name (if not, the process is stil the same). Also, it is assumed that information for one person is stored at the correct corresponding node. So, person1 has all information in first nodes of all 4 lists, person2 has all nodes at "index" 1.

So, you start searching the LinkedList storing names, and stop as soon as you find the name (or throw an error if not found). Also keep track of the number of nodes you have seen. This will be the index you need to get information for all other Lists.

Now, using for loop, traverse appropriate number of times on each of the rest 3 Lists, and return the value encountered.

Community
  • 1
  • 1
Karan Goel
  • 1,117
  • 1
  • 12
  • 24
  • Thank you for you input, this is however a bit under my scope of coding for this specific project. I apologies for my misunderstanding of my own material when I posted it, but I quickly understood what I needed to do. I have edited the OP for the purpose of asking for help with associating information from one linked list to the other. – Ryan Tibbetts Mar 27 '13 at 21:35
  • OK! So basically I can just search for one quality associated with the user and the index should line up within every node! Makes sense XD – Ryan Tibbetts Mar 27 '13 at 21:45
  • So then assuming I were to sort the names alphabetically I would just sort one node in one list at a time and just switch as I sort them. That's a whole nother question I think though... – Ryan Tibbetts Mar 27 '13 at 21:47
  • 1
    Well, if you sort any 1 list, you'll lose all relation between the data. That is why, creating a custom node is a better option. It will be less work in the long run. – Karan Goel Mar 27 '13 at 22:56
0

It would be easier to group all that data in a class and then use a Hashtable as your data structure, you should find many resources online about Hashtable's also check out http://javarevisited.blogspot.com/2012/01/java-hashtable-example-tutorial-code.html. About obtaining input..you can store it in an array since the number of variables is fixed then use Arrays.toList() to cast it into a list and perhaps use a switch inside the loop to initialize your variables. note that there are alternatives to the hashtable structure but i recomend since you want to retrieve your data later and it might get large.

ac3hole
  • 123
  • 2
  • 10
  • Thank you for your suggestion. However, I already know how to use array lists quite decently. I'm trying to learn some new tricks with a different storage method. So I'm not interested in using a hashtable or arrays. – Ryan Tibbetts Mar 27 '13 at 21:42
0

This is a typical Primitive Obsession.

You should use an Object to save the data. It would be more flexible and easy to use.

Sifeng
  • 713
  • 9
  • 23