2

I want to ask you about the print vector array , the following one:

Vector[] routingTable = new Vector[connectivity.length];

I tried this method , but it doesn't work with me and it gives me protocol.Route@c17164 when I printed in the main, here is the code, so can you tell me why it doesn't print the correct value ?

public String printRT(int hop)
{
   String s = "";
   for (int i = 0; i < conf.routingTable[hop].size(); i++) 
  {
     s= " ROUTING TABLE " + conf.routingTable[hop].get(i);
  }
  return s;
}
mac
  • 42,153
  • 26
  • 121
  • 131
  • Ah, this is a much better question than the last one. – Michael Myers May 05 '09 at 19:20
  • 1
    If you are using Java 1.5 or later, you can also simplify your loop to: for (Route currHop : conf.routingTable[hop]) { s = " ROUTING TABLE " + currHop; } – kenj0418 May 05 '09 at 19:23
  • Possible duplicate of [How do I print my Java object without getting "SomeType@2f92e0f4"?](http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Raedwald Jan 24 '16 at 18:01

6 Answers6

7

it looks like you need to implement the toString() method in protocol.Route.

class Route {
    public String toString() {
         return "some string that makes sense";
    }
}
mac
  • 42,153
  • 26
  • 121
  • 131
Cogsy
  • 5,584
  • 4
  • 35
  • 47
  • what I have to add in toString() method? –  May 05 '09 at 19:24
  • 2
    You'll probably want an @Override on that (although the original question is using Vector, so perhaps not). – Tom Hawtin - tackline May 05 '09 at 19:29
  • thanks alot I did what you tell me and it is wark now thanks Alot –  May 05 '09 at 19:29
  • 1
    @shamsa: toString() is a method used to textually represent the state of your object and a lot of methods use it to coerce your object into a String. The javadoc for Object recommends that you always override this, not least of all because that can help you get useful debuging information. – James May 05 '09 at 19:38
6

Either override the toString() method on the protocol.Route class, or get the desired properties from the Route object and append them to the String s inside your printRT method.

erickson
  • 265,237
  • 58
  • 395
  • 493
4

Many helpful suggestions, but I think everyone is overlooking something very simple- in each loop iteration you are overwriting the value of s. I think you mean to say something like the following instead:

s += " ROUTING TABLE " + conf.routingTable[hop].get(i);

Note the "+=" rather than simple assignment. Or use a StringBuilder, or whatever.

mac
  • 42,153
  • 26
  • 121
  • 131
George Armhold
  • 30,824
  • 50
  • 153
  • 232
2

When you ask java to print an object for which no toString method is defined, then it will fall back on the default toString implementation in the Object class. From the javadocs:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())

In your example 'protocol.Route' would be the class name and 'c17164' is whatever the hashcode method returns as a hexString, which, unless hashCode has been overwritten, is probably the address of the object, although this is implementation dependent.

So, there are a few ways to fix your problem.

  1. Write your own implementation of the toString method for the Route class that prints out the data you want. This is probably the most "correct" way to fix your problem. It keeps things nicely encapsulated within the class, meaning only the toString method inside of the class needs to know about the exact member variables that are to be printed.

  2. If the situation is such that you cannot change the Route class, you could subclass your own version of the Route class that you could add a toString method to. However, depending on the design of the class, this may be difficult.

  3. Have the current printRT method look inside each Route object and get the specific information that you want to append to the current string.

Also, note that with the current code, you have written the following in the inner loop:

  s= " ROUTING TABLE " + conf.routingTable[hop].get(i);

This means that printRT will only return a string for the very last iteration of the loop. So most of the time in the for loop is spent creating strings, assigning them to a variable and then overwriting them the next time through the loop.

If you want to return a string representation for every iteration, you will need to change the above to something like the following:

  s += " ROUTING TABLE " + conf.routingTable[hop].get(i);

Now the new information is being appended to s every time through the loop. However, depending on the number of string concatenations being performed, the StringBuilder class may be a better alternative (see a short summary and tutorial on it here).

Paul Wicks
  • 62,960
  • 55
  • 119
  • 146
1

Two options.

Either override the toString() method on the protocol.Route class.

public String toString() {
    return someMethodorPropertyThatreturnsString;
}

or get the desired properties/methods from the Route object and append them to the String s inside your printRT method.

public String printRT(int hop)
{
  String s = "";
  for (int i = 0; i < conf.routingTable[hop].size(); i++) 
  {
     s= " ROUTING TABLE " +     conf.routingTable[hop].get(i).someMethodorPropertyThatreturnsString;
  }
  return s;
}
mac
  • 42,153
  • 26
  • 121
  • 131
rick schott
  • 21,012
  • 5
  • 52
  • 81
0

There are a number of issues here.

  • You should be specifying a type to put in your List with Generics. That way, you will make it more obvious to yourself and others what you are putting into and taking out of your List.

  • As mentioned by others, your List is a list of protocol.Route objects, not Strings. When you try to add a Route to s, Java doesn't know how to convert it into a String, so it uses the default Object#toString(). Override it in Route to do what you want.

  • It looks like you'll potentially be doing a lot of appending here. Use a StringBuilder.

  • It looks to me like printRT(int) should be a method inside of whatever conf is.

  • You should probably be using a different implementation of List; Vector is not really recommended to use anymore, so take a look at other options like ArrayList.

Adam Jaskiewicz
  • 10,934
  • 3
  • 34
  • 37