-
beginner
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!)
Code:
class Customer implements Comparable<Customer> {
private String sequance;
public Customer() {
sequance = "ppp";
}
public Customer(String string) {
}
public Customer(String N, String S) {
this();
sequance = N;
}
public String getName() {
return sequance;
}
public void setName(String letters) {
this.sequance = letters;
}
public void print() {
System.out.println(sequance + "\t" );
}
@Override
public int compareTo(Customer o) {
return this.sequance.compareTo(o.sequance);
}
}
public class Main {
public static void main(String[] args) {
Customer[] cust = new Customer[10];
String name[] = { "ccc", "ddd", "bbb", "ddd", "aaa", "Alex",
"Andrej", "Oleg", "Ivan", "Egor" };
int i;
System.out.println("All customers:");
for (i = 0; i < cust.length; i++) {
cust[i] = new Customer();
cust[i].setName(name[i]);
cust[i].print();
}
System.out.println("Sorted alphabetically: ");
char alph[]={'a', 'b', 'c', 'd', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
for (int x=0; x<alph.length; x++)
{for (i=0; i<cust.length; i++)
{
if (cust[i].getName().charAt(0)==alph[x])
cust[i].print();
}
}
}
}
-
Use the array's sort method.
Arrays (Java Platform SE 6)
ArrayList (Java Platform SE 6)
Its much simpler, and you can look at examples here: sorting strings into alphabetical order - Java
Ignore the second post, its not really worth reading.
-
Even better: use a TreeSet because you don't want any duplicates.
kind regards,
Jos
-
If this is a class project/homework he might not be able to go and use things that haven't learned about yet. However if its not then I agree with JosAH, look into that.
If it is a class assignment you could check to see if the entry already exists by looping through your array and use a .equalsIgnoreCase() to compare if a string has already been entered.
-