0

In one of my classes, I have something like this:

public Foo(int id, String client, String contents) {
    this.id = id;
    this.client = client;
    this.contents = contents;
}

public String toString() {
    return id + contents + client;
}

I use the id number as a key in the Java API HashTable and I use the object Foo as a value. In my next class, I want to print all my keys and the values, so I try

Hashtable<Integer, Object> hashtable = new Hashtable<>();
Enumeration<Integer> keys = hashtable.keys();
Collection<Object> values = hashtable.values();
while(keys.hasMoreElements() ){
    System.out.printf("%-5d%-5s", keys.nextElement(), values.toString());
}

Now, that doesn't quite do what I want it to do. I want my values to be evenly spaced like this:

ID #1        ID #1          client #1          content #1
ID #2        ID #2          client #2          content #2
ID #3        ID #3          client #3          content #3

I can't fix it in my toString class because the values won't be evenly spaced there and also because I am using that specific representation of Foo somewhere else in my program.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
James
  • 83
  • 1
  • 6
  • 1
    So is your question really about how to iterate, or about how to format the results. (**Don't say "both"** ... because that makes your question "Too Broad"!) Once you have decided which, [EDIT](https://stackoverflow.com/posts/67145531/edit) the question to make it clear what you are really asking. (For example, if this is about formatting, then the question title is misleading.) – Stephen C Apr 18 '21 at 05:55
  • Your problem seems to be about formatting (and not iterating). Possible duplicate of https://stackoverflow.com/questions/6431933/how-to-format-strings-in-java and https://stackoverflow.com/questions/22416578/how-to-use-string-format-in-java. – Viral Lalakia Apr 18 '21 at 05:58
  • Hint: when you search, it is important to search for the right thing. If your problem is formatting but you search for "how to iterate a hashtable", then you will get Q&A's about ... iterating, not formatting. – Stephen C Apr 18 '21 at 06:01
  • 1
    Not related to your problem, but `Hashtable` is considered outdated, it is usually better to use `HashMap`. – Mark Rotteveel Apr 18 '21 at 12:13

1 Answers1

1

First, make sure you can access to your Foo's members. Define a public getter on each members:

public Foo(int id, String client, String contents) {
    this.id = id;
    this.client = client;
    this.contents = contents;
}

public int getId() {
    return this.id;
}

public String getClient() {
    return this.client;
}

public String getContents() {
    return this.contents;
}

Then represent it like you want into your while loop :

while(keys.hasMoreElements()) {
    String key = keys.nextElement();
    Foo value = hashable.get(key);

    System.out.println(value.getId + "    "  + value.getClient + "    "  + value.getContents);
}
Xavier Brassoud
  • 697
  • 6
  • 14