1

I'm new to Java programming and I'm following a guide on Udemy. I'm working with data manipulation through classes, this one being a simple name formatter. Predefined strings (parts of a name) are inserted into the class to format them.

This program uses two classes: Name.java and NameTest.java (the driver file)

Name.java:

package javaProject;

    public class Name { 
        private String first;
        private String middle;
        private String last;

        public Name(String f, String m, String l){
            first = f;
            middle = m;
            last = l;
        }

        public Name(String f, String l){
            first = f;
            middle = "";
            last = l;
        }

        public Name(String f){
            first = f;
            middle = "";
            last = "";
        }

        public Name(){
            first = "";
            middle = "";
            last = "";
        }
        ///
        public String displayName(){    
            return first + " " + middle + " " + last; 
        }


    public static void main(String[] args){

    }

}

NameTest.java:

package javaProject;

public class NameTest{
    public static void main(String[] args){
        Name myName = new Name("Damon", "myMiddleName", "myLastName");
        System.out.println("My Name: " + myName.toString());
    }

}

Output:

My Name: javaProject.Name@35afe17b

Any idea why I'm getting this after specifying that the output should be a string? I'm not exactly sure how to fix this because, again, I'm really new to Java.

Zulfe
  • 820
  • 7
  • 25
  • 1
    You need to define your own toString(), usually your IDE can do this for you as it's rather tedious. Why Java doesn't do this for you is likely to be historical. It gives you the class name and the identity hashCode. – Peter Lawrey May 06 '14 at 02:59
  • Possible duplicate of [How to support println in a class?](http://stackoverflow.com/questions/27647567/how-to-support-println-in-a-class) – Raedwald Mar 26 '16 at 14:10
  • Possible duplicate of http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4 – Raedwald Mar 26 '16 at 14:19

3 Answers3

4

Rename your method, displayName does not override Object.toString() which is what you want;

// public String displayName(){ // <--- toString()
public String toString() {    
  return first + " " + middle + " " + last; 
}

There is a second option though (but note the advantage of the toString()), and that is

public static void main(String[] args){
    Name myName = new Name("Damon", "myMiddleName", "myLastName");
    System.out.println("My Name: " + myName.displayName()); // <-- or this,
    // System.out.println("My Name: " + myName); // <-- but the advantage of performing
                                                 // <-- an override on toString() is
                                                 // <-- that this implicit `toString()`
                                                 // <-- will work.
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

you have to override toString()

something like

public String toString()
{
return first+" "+ middle+" "+last;

}
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
1

Your class name lacks a toString() method, and inherits the default one from Object which prints the class name and memory address.

Replace:

public String displayName(){    
    return first + " " + middle + " " + last; 
}

With:

public String toString() {    
    return first + " " + middle + " " + last; 
}
Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
  • 2
    Wow, what an odd coincidence. The tutorial had named the formatting method 'toString' while I named mine 'displayName'. 'toString' just so happened to share a method name with something completely different. I switch 'myName.toString()' to 'myName.displayName()' and it works great! – Zulfe May 06 '14 at 03:00