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

beginner

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi!
I am very young and very inquisitive!
I the beginning programmer.
My program
(Sorting alphabetically)
input file:
ccc
ddd
bbb
ddd
aaa

output file:
aaa
bbb
ccc
ddd
ddd


How to make so that in a target file there were no repetitions?
that was like this:
aaa
bbb
ccc
ddd


Help please!)


 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,

First of all the code is not sorting (rather printing ) all the names of the input customer array.

You can use the Set Interface to make sure only one String (no redundant) is stored in it. You can read the documentation here. Set Interface
Since you are pretty new to Java see below modified code to use Set interface in your program.

Please let us know if you coded the Customer class that implements Comparable interface. As I think the sort can be done easily if we use collection of customer objects rather than array of customer objects.

Please see the output below from your code and can you figure out why some names are missed?

Output
=======
ccc
ddd
bbb
ddd
aaa
Alex - missing after code does sorting
Andrej - missing after code does sorting
Oleg
Ivan
Egor

Sorted alphabetically:
aaa
bbb
ccc
ddd
Egor
Ivan
Oleg



And welcome to Ranch petr pavlovich
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Unless this is a homework assignment, you can use the Collections framework for this. java.util.Collections has two methods to sort any List. So store the elements in a List and call one of those two methods.
To filter out any duplicate elements, use a TreeSet. That has automatic sorting, and also automatic duplicate filtering.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Petr, welcome to theRanch! We don't have too many rules around here, but we do ask that you BeForthrightWhenCrossPostingToOtherSites

http://www.java-forums.org/new-java/46286-beginner.html
 
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

petr pavlovich wrote:Hi!
I am very young and very inquisitive!
I the beginning programmer.
My program
(Sorting alphabetically)
input file:
ccc
ddd
bbb
ddd
aaa

output file:
aaa
bbb
ccc
ddd
ddd


How to make so that in a target file there were no repetitions?
that was like this:
aaa
bbb
ccc
ddd


Help please!)




Hi, do you know how to sort name from 2nd word?
Example :

input :

aaaa bbb
ccc aaa
bb ddd

so, the output would be like :

ccc aaa
aaaa bbb
bb ddd

?
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please don't ask the same question twice. I don't think you are going to get an answer from the OP, but I am going to close this discussion anyway to prevent duplication of effort. I have given you links where you can find answers anyway, in your other thread.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic