aspose file tools*
The moose likes Beginning Java and the fly likes beginner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "beginner" Watch "beginner" New topic
Author

beginner

petr pavlovich
Greenhorn

Joined: Jul 10, 2011
Posts: 22
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!)


John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4544
    
    5

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


luck, db
There are no new questions, but there may be new answers.
 
 
subject: beginner