aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enhanced For Loop; How to Properly Format Output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Enhanced For Loop; How to Properly Format Output" Watch "Enhanced For Loop; How to Properly Format Output" New topic
Author

Enhanced For Loop; How to Properly Format Output

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
From Chapter 5, Sierra/Bates, Enhanced For Loop.

I understand the following:



Tried and modified code from the same section:




Please notice the lines 23 and 35 with the comment "causing weird output".

Output of this program is:

7
8
9
4
5
6
[I@446b7920
[I@6bdd46f7
[I@c5e3974

7
8
9
one
two
three
one
two
three
Dog@4650d89c
Cat@65bd0dd4


Having difficulty finding a way to properly display the contents of twoDee and animals. Thanks for any input.

Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
This is not a for loop issue but rather a toString() issue as you're seeing the returns from the default toString() of your Animal class and arrays. Give your Animal class a decent toString override, and that issue is solved. For arrays, it will take a little futzing.

Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316


You are misleading two things :-

1) Line 23 : - In line 23 your are trying to print n which is referring to an Object ie int[] and hence shows hascode value.

2) Line 35 :- You are trying to print a , which is first referring to dog and then to cat. Remember System.out.println() uses toString() method while printing its argument object. So when it tries to print Dog object , it would print its Inherited method from Object class, which says to print the hashcode value. Similar is the case with code.
If you want to print something then you must override toString() method in dog and cat class, which would be used during printing.

Thanks !!!
Hope you got my point !!!
Happy Learning !!!
Best Wishes !!!


SCJP 6.0 96%

(Connecting the Dots ....)
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561

Probably not good practice to hard-code this. Do you know about the toString utility methods of the java.util.Arrays class?
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
@pete stein

totally agree with you sir.

No sir, i was unaware of that. Thanks, now it is added to my API's !!!

Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
@pete stein


What about this practice sir???
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Hello,

Thanks for the guidance. Below is working code, along with follow-up question:




I've been trying to use the toString method to display contents of twoDee. For instance, instead of using





I attempted to use




and




but it still prints "weird" output

Please clarify


sebastian tortschanoff
Ranch Hand

Joined: Mar 19, 2009
Posts: 68
Hi Sandra,

watch this:



What are you printing? Not any value within an Array, but object references. With this enhanced for-loop you're getting a Array-Reference from a twodee-Array. And then you print it. Hence you will get some memory address like this: I@446b7920

Consider this:



What are you printing here? Values of type long.

understood?

Power from within.

Failed SCJP 2 times :-(
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
@sebastian: I'm sorry to read that you failed the SCJP. Seems like a really tough exam. I'm sure you will Ace this exam really soon!

I'm still unclear on how to use the .toString() function to display the contents of an array, as was discussed earlier in the thread. Or whether it is even possible, and using the .toString() function simply means to display the reference in string form, hence the "weird output"
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Sandra Bachan wrote:
I'm still unclear on how to use the .toString() function to display the contents of an array, as was discussed earlier in the thread.


The examples above don't use the array toString but use the toString method from the java.util.Arrays class.
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
The examples above don't use the array toString but use the toString method from the java.util.Arrays class.


@pete stein: Where can one find examples that use the array toString, i.e. such that toString will display the contents inside the array? Is there such a concept in Java 6? Please guide.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Sandra Bachan wrote:@pete stein: Where can one find examples that use the array toString, i.e. such that toString will display the contents inside the array? Is there such a concept in Java 6? Please guide.


It's the Arrays class, not array, and you can find more about its methods at its api: Arrays

Also note the deepToString method which is useful for multi-dimensional arrays.
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
I'm going to re-visit this after reading Chapter 6, Sierra/Bates, Strings. Concept seems challenging.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enhanced For Loop; How to Properly Format Output
 
Similar Threads
for static
Regarding Arrays
Static method : Redefinition or an override ?
Tricky Question about Continue Statement
Casting to an Interface Type