1

I have been working with a comparable class that runs and compares correctly, but when I try to print the values out, it gives me the memory addresses. I know that this could be fixed with a toString method, but I'm not really sure how to go about doing so. Here's my code:

import java.io.*;
import java.util.*;
import java.lang.Object;

public class StreetAddress implements Comparable<StreetAddress>
{
    protected int num;
    protected String stName;

    public StreetAddress(int n, String s){
    num = n;
    stName = s;
    }

    public int getNum(){
        // returns the street number
        return num;
    }

    public String getName(){
        // returns the street name
        return stName;
    }

    public int compareTo(StreetAddress street) throws ClassCastException{
        // exception prevents crash if an address is not compared to
        // another address
        int compareName = this.stName.compareTo(street.stName);
        int compareNum = this.num - street.num;
        if (compareName < 0){
            // first address comes after compared address
            return compareName;
        }

        else if (compareName == 0){ // same address name
            if (compareNum < 0){
                System.out.println(this.num + "" + this.stName + ", " + street.num + "" + street.stName);
                return compareName;
            }
            else{
                System.out.println("");
                return compareName;
            }
        }

        else{
            // first address comes before compared address
            return compareName;
        }

        }

    public static void main(String args[])
    {
    StreetAddress add1 = new StreetAddress(7864, "cartesian road");
    StreetAddress add2 = new StreetAddress(5141, "cartesian road");
    StreetAddress add3 = new StreetAddress(1664, "n kings street");
    StreetAddress add4 = new StreetAddress(9753, "pioneer parkway");
    StreetAddress add5 = new StreetAddress(3643, "starry avenue");
    add1.compareTo(add2);
    add4.compareTo(add1);
    add3.compareTo(add3);
    add2.compareTo(add5);
    }
}
arshajii
  • 127,459
  • 24
  • 238
  • 287
Ampage Grietu
  • 89
  • 1
  • 2
  • 10

2 Answers2

5

Simple:

@Override
public String toString() {
    return String.format("%d %s", num, stName);
}

The @Override annotation is not necessary but it is definately good practice. Note that String.format("%d %s", num, stName) results in a string of the form "[num] [street]". I find that String.format gives greater flexibility than string concatenation if you want to make changes in the future.

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • Shouldn't this also include the street number, and not just the street name? – templatetypedef Apr 07 '13 at 23:13
  • @A.R.S. So what exactly does the "%d %s" mean? I haven't seen that used before. – Ampage Grietu Apr 07 '13 at 23:37
  • @AmpageGrietu The `%d` gets replaced by the int argument (`num`) and the `%s` gets replace by the string argument (`stName`). You can read more about the format string syntax [here](http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax). – arshajii Apr 07 '13 at 23:37
2
@Override
public String toString() {
   return "StreetAddress: num= " + num + ", stName=" + stName ;
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276