• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Enhanced For Loop; How to Properly Format Output

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 !!!
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@pete stein


What about this practice sir???
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to re-visit this after reading Chapter 6, Sierra/Bates, Strings. Concept seems challenging.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic