-1

I have a class Employee which has a method that returns a 2D array with some employee information. I have another class TestEmployee in the same package where I create an object test of the class Employee and then print this object.

Employee test = new Employee();
System.out.println(test);

Now this test object should print the array I created in Employee. I am not able to write the code to do that. Can someone please help me with this question?

Thanks

Johan Falk
  • 4,341
  • 2
  • 30
  • 42
k19
  • 83
  • 3
  • 10
  • 1
    make a toString() method for employee, and do `System.out.println(test.toString());` – Davis Broda Apr 08 '15 at 17:59
  • Davis, Is there a way that toString() does not have to be called? I mean I don't have to write test.toString() and only test. – k19 Apr 08 '15 at 18:32
  • you could make/use a method to retrieve the 2D array, and then use `Arrays.toString()` inside of a for loop. Here's a link to a question that has a good example of that: http://stackoverflow.com/questions/2397535/print-two-dimensional-array-of-strings-as-string – Davis Broda Apr 08 '15 at 18:44
  • Related: http://stackoverflow.com/questions/328661/explicit-vs-implicit-call-of-tostring, http://stackoverflow.com/questions/17051481/how-an-object-will-call-tostring-method-implicitly (implicit calling of `toString()`), and also http://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java (how to use `toString()`). – Jason C Apr 09 '15 at 00:33

2 Answers2

0

I know this is extremely late but you use deepToString() to print out a multidimensional array.

-1

I don't know why someone would downvote this question.

Employee is an Object type, and you can't print Objects and expect good output because it prints out its hash code (thanks Obicere).

Well, unless you override the toString() method. If you do, and you do it well, you can do:

System.out.println(test.toString());

I'm assuming you want to print out the information contained in the 2D array. If that's so, then you'd want to parse all that information into a single String and have your toString() method return that.

Cheers,
Justin

theguywhodreams
  • 315
  • 1
  • 8
  • Thanks for your comments. Yes, my TestEmployee class has the main method. Suppose I write a toString() method. Is there any way, I don't have to do the test.toString() to call. I mean can it be called directly? – k19 Apr 08 '15 at 18:31
  • Erm, you can try doing it directly once you've overridden toString(). I'm not sure if it'd work though since I've never really tried myself, but it's worth a shot. – theguywhodreams Apr 08 '15 at 18:34
  • 1
    `or some hex number I don't know.` Its actually the `hashCode()` value. Also, `System.out.println(test);` will call `test.toString()` after performing a null-check. There is no notable difference between your code and the answer's code. – Obicere Apr 08 '15 at 18:59
  • Oh, okay. TIL. Either way, she'd still have to override the toString() method to return whatever is in those 2D arrays of hers. Thanks for the new knowledge, lol. – theguywhodreams Apr 08 '15 at 19:02
  • @theguywhodreams : I was able to print out directly after overriding the toString() method. Thanks – k19 Apr 08 '15 at 20:42