Printing an Object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Palash
    New Member
    • Apr 2007
    • 2

    Printing an Object

    Hi,
    I have a constructor
    public Road() {
    name = DEFAULT_NAME;
    number = DEFAULT_NUMBER;
    length = DEFAULT_LENGTH;
    String read="";
    read += toString(name,n umber,length);
    // toString is a function which formats the data into "DEFAULT_LENGTH ,DEFAULT_NAME,D EFAULT_NUMBER, DEFAULT_NUMBER" ;

    }
    Then in main I have
    Road roadOne = new Road();
    System.out.prin tln(roadOne);
    How do i get it so that it will print "DEFAULT_LENGTH ,DEFAULT_NAME,D EFAULT_NUMBER, DEFAULT_NUMBER" , without changing anything in main
  • BSCode266
    New Member
    • Jan 2007
    • 38

    #2
    If Road has a toString you should use this one.

    System.out.prin tln(roadOne.toS tring());

    BSCode266

    Comment

    • Palash
      New Member
      • Apr 2007
      • 2

      #3
      Originally posted by BSCode266
      If Road has a toString you should use this one.

      System.out.prin tln(roadOne.toS tring());

      BSCode266
      I cant change anyting in main only in my function

      Comment

      • BSCode266
        New Member
        • Jan 2007
        • 38

        #4
        Originally posted by Palash
        I cant change anyting in main only in my function
        I dont think its actually possible without changing the System.out.prin tln.

        Because what you are getting now probably is Road@etc etc.

        if it is i'd like to know about it to.

        BSCode266

        Comment

        Working...