Hello there. Could you give some insightful input into my problem:

My program is fed multiple lines of input. The input is composed of a name and two values of that person. Each line is of a new 'person' e.g.

"James 23 15.05
Lucy 51 25.523
Kam 42.4 21.54
Jak 85 242" etc.

There are two requirements:

  1. I must iterate through each line, and read the name, one by one. I won't know what the name is, but I will know how many names there are. I would iterate through it with a forloop.
  2. There must be some "key"-like variable or something, and this key will be associated with the name and the values.

What I have thought of:
A HashTable... but I ran into the problem where you cannot iterate through it easily like through a forloop.

A 2D array would be too cumbersome.

What would be a better solution to meet my requirements, and how would I use it?

(Even better... You may have noticed that each new line of input contains 3 values: the name, and two values. Is there something where I can map the three values? At the moment, my soon-to-be-replaced-with-something-else HashTable looks like this: hashT.add("James", "23 15.05") where the key James is associated with one value of "23 15.05". Is there something to enable me to key James with "23" AND "15.05"?)

Thank you!

Recommended Answers

All 3 Replies

Yes, a person object

public class Person{
  private String name;
  private int age;
  private double aValue;

 //class constructors and getters to be added
}

put this object in collection and magic done

Yes, a person object

public class Person{
  private String name;
  private int age;
  private double aValue;

 //class constructors and getters to be added
}

put this object in collection and magic done

Thanks for the awesome answer, peter_budy, I can finally progress. I am unfamiliar with 'collection', but don't think I"ll need to use that; I am happy to just create a "Person" object for now, and manipulate things through that.

Anyhows, now I have run into another problem:
I need to collect the Person information while in a for-loop. E.g.

for(int i = 0; i < numOfPeople; i++){
   //Make a [B]unique [/B]Person object
   //do other things...
}

My problem here is that I don't know how to make a unique Person object name. (I was unable to do "Person i = new Person();" because "i" was already taken by the forloop.)
How can I get around this, to create new Person objects in the forloop?

Thanks!

Basically you can't create a new variable name for each Person object like that.
That's where the collection comes in. You need a collection of Person objects.
At your stage it may be better to stick with a simple array of Persons to hold your collection; you can learn about the Collection classes such as ArrayList later.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.