-2

I'm trying to print out an object's instance variable's contents, but I'm stuck on how to do that. Here's my .java file where I ask to fill the array

for (int i = 0; i < friends.length; i++) {
        friends[i] = new Datab();
        System.out.println("\nFilling object #: " + (i + 1));
        Scanner kb = new Scanner(System.in);
        System.out.println("Enter First name: ");
        first = kb.next();

        System.out.println("Enter Last name: ");
        last = kb.next();

        System.out.println("Enter Cell Number: ");
        cell = kb.next();

    }

And here's the .java file with the constructor and the toString

  protected static String first;
        protected static String last;
        protected static String cell;
        private String what; 

    public Datab()
    {
        first = null;
        last = null;
        cell = null;
    }

    public Datab(String f, String l, String c)
    {
        first=f;
        last=l;
        cell=c;

    }
    public static String getFirst(Datab [] n)
    {


        return first;   
    }
    public static String getLast(Datab [] n)
    {

        return last;  
    } 
    public static String getCell(Datab [] n)
    {


        return cell;

    }



 public String toString(){




     return("");

 }
   }
  • `return("");` There's your problem..... ;) Why don't you just make use of the various `get` methods already implemented in your class? – tnw Mar 30 '15 at 15:58
  • Are you asking how to implement `toString` method in your class? – Luiggi Mendoza Mar 30 '15 at 15:58
  • Where are you stuck? You are returning an empty string from `Datab.toString()`, so, for starters, it doesn't appear like you've tried anything. It's also worth noting that at no point do you seem to be setting the values of the fields in the new `Datab` you're storing in `friends[i]`. For an entire array, after you've properly implemented your class's `toString()`, see [`Arrays.toString()`](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(java.lang.Object[])). – Jason C Mar 30 '15 at 15:59
  • Yes, and to have it print out my friends object. – Robert Ducharme Mar 30 '15 at 16:00
  • If you want to output the complete array look at this question: http://stackoverflow.com/questions/10904911/java-how-to-convert-int-array-to-string-with-tostring-method – Kazaag Mar 30 '15 at 16:07

1 Answers1

0

You are overriding the toString correctly but it doesn't give you anything. If you want it to give you meaningful data about the object, here is an example of how you would go about that

@Override
public String toString() {
   return "first:"first + " last:" + last + " cell: " + cell; 
}

All you have to now is make a call to System.out.println, Say System.out.println(friends[i]); which makes a implicit call to that object's toString method which will print out the state of the object.

committedandroider
  • 8,711
  • 14
  • 71
  • 126
  • Thanks for the answer. I see what you're saying, but if I do that it just returns what first last and cell most recently were. I want to print all the names and numbers. How do I go about that? – Robert Ducharme Mar 30 '15 at 16:14
  • What numbers. This toString includes the state of all your instance variables. – committedandroider Mar 30 '15 at 18:20