Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Printing Object details in for-each loop

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

What am I doing wrong?

I am expecting output that is a combination of Echo, Foxtrot, Golf but instead I get

*** Demo using an Object 'TestSort' ***
Before InnerComparatorSort3 sort = chapter07.TestSort@3e25a5chapter07.TestSort@19821fchapter07.TestSort@addbf1
After InnerComparatorSort3 sort = chapter07.TestSort@3e25a5chapter07.TestSort@19821fchapter07.TestSort@addbf1
Before OuterComparatorSort sort = chapter07.TestSort@3e25a5chapter07.TestSort@19821fchapter07.TestSort@addbf1
After OuterComparatorSort sort = chapter07.TestSort@3e25a5chapter07.TestSort@19821fchapter07.TestSort@addbf1


 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you need to override the toString() method in your TestSort class. Also you are calling toString() on an Object, this will print out the reference to the screen.

Hunter
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is java supposed to know what you want your object to behave when you call its toString() method? What if your object had two Stirng? or eight? or none?

You have to provide the version of toString() that makes sense. You get the default version from the Object class, but most of the time, that isn't terribly useful (as you can see).
 
Scott Gordon Sutherland
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have previously tried overriding String but I was not much better off. I've tried the following, along with the changes these require to the class and Arraylist delcaration (ie in line with the JLS 14.14.2)


This is better in that it gives me a readable output. But this is also incorrect as all I get is "...Foxtrot Foxtrot Foxtrot". In the code above I was trying to go back a step or two to sort out where I could have lost the plot.

Perhaps I need to approach this from another angle - my understanding is that

Will iterate through the collection "alist3" returning each item in the collection, which can then be printed. However I cannot seem to be able to access each value in the ArrayList. Experiments with alternatives such as


While all were found in examples/tutorials, they either don't compile, or also just repeat the last item.

Can you guide me on what I am missing here - Perhaps even a link to a tutorial on this? Everything I have founn seems to focus only on using for-each with basic Arrays of String values. So many things work but that does not translate when custom objects are used.

Regards
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if your alist objects are full of TestSort objects, you should iterate through each alist and call toString() on each TestSort inside the list:



Hunter


EDIT: fixed some spelling errors
 
Scott Gordon Sutherland
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Thanks for the idea, but


Just generates a compile error noting that the TestSort is unacceptable and that an Object is requried.

FYI - changing the code as follows returns "...Foxtrot Foxtrot Foxtrot"


The toString override was tried with

and


Any more ideas?

Thanks
 
Saloon Keeper
Posts: 10835
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

NOTE: 'str' should not be 'static' and getValue() should not be 'static'.
 
Scott Gordon Sutherland
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Carey

Thanks for the suggestions, but surely the option to implement generics has no relevance in this case. The code is trivial and no other objects exist than TestSort. So while this would be very useful in making ode type safe in a fully fledged programme, in this case there is no possibility for another object being passed into the ArrayList and no code does or will ever exist in this to pass an incorrect type to the the ArrayList.

Also the variable "str" and method getValue must be static, otherwise they cannot be accessed from the static ... Main method.

Regards
Scott
 
Carey Brown
Saloon Keeper
Posts: 10835
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, declaring 'str' as static means that there is only one copy. Hence alist3.add(new TestSort("Foxtrot ")); will overwrite all copies of TestSort with the string "Foxtrot".
 
I don't like that guy. The tiny ad agrees with me.
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic