• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Check if element is unique

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

I'm trying to readin a txt file (CSV, semicolon separated). Each line will be split in to an array.
Now I want to check the first element which should be an unique ID. But for a reason it does not work.



Output:
Line: 1 First Element of current line: 923739
Line: 2 First Element of current line: 923739
Line: 3 First Element of current line: 924005
Line: 4 First Element of current line: 924007
Line: 5 First Element of current line: 924039
Line: 6 First Element of current line: 924286
Line: 7 First Element of current line: 924327
Line: 8 First Element of current line: 924396
Line: 9 First Element of current line: 924664
Line: 10 First Element of current line: 924718
Line: 11 First Element of current line: 924923
...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 52 does not look correct:

Carefully read the API documentation of Collections.frequency(...) and think about what you're doing on that line.
 
Robert Sta
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Line 52 does not look correct:

Carefully read the API documentation of Collections.frequency(...) and think about what you're doing on that line.



Hi Jesper,

thank you for your comment.
Well, I changed the line 52 to:


The list should be the collection and the object should be the current the first element of the current line so values[0].
But it still does not work...

In theory I guess that I have to add all available values (read in the whole file) to the list and afterwards the unique check could be done?
 
Sheriff
Posts: 9057
667
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robert Sta wrote:But it still does not work...


Please specify: got compilation error? run-time error? incorrect output? (in case of latter what was the output? what were expectations?)
 
Robert Sta
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robert Sta wrote:

Jesper de Jong wrote:Line 52 does not look correct:

Carefully read the API documentation of Collections.frequency(...) and think about what you're doing on that line.



Hi Jesper,

thank you for your comment.
Well, I changed the line 52 to:


The list should be the collection and the object should be the current the first element of the current line so values[0].
But it still does not work...

In theory I guess that I have to add all available values (read in the whole file) to the list and afterwards the unique check could be done?



Finally I got it!


I changed my code to:

 
Marshal
Posts: 81617
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My, that is a long method; it shou‍ld be divided up. I think you can make about six methods out of it.
Declare your buffered reader as a local variable; it doesn't need to be a field. In fact having it as a field is error‑prone because there is a risk of your trying to use a closed Reader.
There is something not quite right about using an int as status. Use an enumerated type instead.

I would have thought that Collections#frequency will run in linear time if applied to a List. Consider instead adding the Strings you are reading to a Set<String> and testing the return value from the add() method. That can run in amortised constant time or, in the best possible situation, plain simple constant time.
If you compare execution times with number of items to read, the whole procedure will probably run in quadratic time with frequency and linear time with Set#add.
 
And inside of my fortune cookie was this tiny ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic