• 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 unique names in alphabetical order from text file

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a program that I am to read authors names, ISBN#, and the publishers name; with only the publishers name printing to the console only unique publishers and in alphabetical order. I have the publishers printing to the console, however, my final output is not grabbing all publishers for alphabetizing them nor is it taking out the duplicates. I have worked on this for days now, and am going in circles. Can someone point out to me the possibilities of what is wrong? Thank you!

Certain portions of this post has been edited to uphold the integrity within teachings of Java programming.

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So for each line in the file, you read the data and get the publishers from it. (There's only one, isn't there?) You sort them and print the unique values.

But that wasn't what you wanted to do, was it? How does that description differ from what you wanted to do?
 
Kimberly Mack
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:So for each line in the file, you read the data and get the publishers from it. (There's only one, isn't there?) You sort them and print the unique values.

But that wasn't what you wanted to do, was it? How does that description differ from what you wanted to do?



Hi Paul, I apologize for any misinterpretation, but my outcome is:


OK, so far, I have all publishers printing out (not really needed). What is needed for this is for the UNIQUE publishers (only) to print to console in alphabetical order. As you see, the Publishers are: and Ascending Oder: print outs are headed in the right direction, but it is only grabbing 1 publisher from my list. :( Hope this is clearer :). Thanks!
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you just create a new TreeSet object (outside your readline while loop) and then add the publisher values to it( inside the loop). You will have a sorted set(without duplicate publishers) as soon as your loop finishes.
 
Kimberly Mack
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kuldeep Tewari wrote:Why don't you just create a new TreeSet object (outside your readline while loop) and then add the publisher values to it( inside the loop). You will have a sorted set(without duplicate publishers) as soon as your loop finishes.



Hi Kuldeep,
Are you suggesting that I hard code my publishers names within the while loop? I really did not want to do that, but if you think it is necessary, I will do that.

Thank you
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think Kuldeep is suggesting anything of the kind. The publisher values can still be read from database.txt, same as before. You create the set outside the loop, but inside the loop, you add each publisher to the set, once. After the loop is done, you loop through the set, and the publishers will be sorted, with duplicates removed. That's what TreeSet does.
 
Kimberly Mack
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the elaboration on Kuldeeps comment Mike. I have tried this what you are suggesting, but will attempt further manipulation.

Thanks again
 
Kuldeep Tewari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike explained, what I wanted to convey. I think you'll need to re-factor your code, a bit. It will be better if you can debug your code line-by-line to see the values contained in your variables(lists etc.).

Lines 43-45 in your code don't seem to do anything that will solve your objective.

List < String > list = Arrays.asList(publisher);
Collections.sort(list);
printNonDuplicates (list);



for each while loop iteration:
1) you are creating a List object having the single String object having the publisher name(extracted for each line in your data file).
2) calling sort on the above list having only one element(It will have no effect as you've only one element in the list).
3) and then passing this single-element list object to your method. As your list contains only one string object, this method just ends up printing the publisher name that was set on line 43 to console.

and these 3 steps are performed for every line in your data file.

Instead,
i)try defining your List before while loop.
List < String > list = new ArrayList<String>();
ii) comment lines 43 and 44 and put list.add(publisher);
iii) move printNonDuplicates (list); after while loop.
iv) change code in line 73 to : Set < String > set = new TreeSet < String > (collection );

Hope it helps.
 
Friends help you move. Good friends help you move bodies. This tiny ad will help:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic