Thursday, May 5, 2011

print method in java

hello every body , 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;
}
From stackoverflow
  • 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.

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

    class Route {
        public String toString() {
             return "some string that makes sense";
        }
    }
    
    Tom Hawtin - tackline : You'll probably want an @Override on that (although the original question is using Vector, so perhaps not).
    James : @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.
  • 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;
    }
    
    Steve Kuo : You don't need the "this" if it's a method.
  • protocol.Route@c17164 is the string returned from the toString() in java.lang.Object. We don't know ( as per your comment ) what useful data you have in your Route class that you would like printed out.

    May be something like

    class Route {
        public String toString() {
             return "from IP: " + this.fromIP + " to IP: " + this.toIP;
        }
    }
    
  • 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.

  • 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).

  • 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.

0 comments:

Post a Comment