oldAccount text file is Nicholas, Diana, Eric,Andy, Alex , AMy
CurrentAccount text file is Andy Alex Amy Kelvin cherry Betty
import java.util.*;
import java.io.*;

public class Example2
public static void main(String[]args) throws IOException
System.out.print (" Old Account is: ");
TreeSet<String> oldAcc = new TreeSet<String>();
Scanner oldacc = new Scanner(new FileReader("OldAccount.txt"));
while (oldacc.hasNext()) {
String Line =oldacc.nextLine();

System.out.print (" Current Account is: ");
TreeSet<String> CurrAcc = new TreeSet<String>();
Scanner curracc = new Scanner(new FileReader("CurrentAccount.txt"));
while (curracc.hasNext()) {
String Line1 =curracc.nextLine();


TreeSet<String> GradAcc = new TreeSet<String>();

System.out.print("The Grad Account is " + GradAcc);


The out put of GradAcc should is Diana , Eric , Nicholas
But My output is Alex ,Andy , Diana , Eric , Nicholas

any solution to solve???

Recommended Answers

All 10 Replies

Try using trim on the Strings before you add them to the sets.

Example of Trim is

to be found in the API docs.

The way is use constructor

public TreeSet(Comparator<? super E> comparator)

, and write own String comparator where the case of letter is irrelevant

Uhm, no. He's using Strings and they are already comparable.

at where i need use the string trim ?

Between reading the String and adding it to set, of course. Do you need someone to hold your hand when using the bathroom?

String Line =oldacc.nextLine();

iziit somethings like this ?

trim returns a String (as the API docs state), so where are you storing that String? Strings are immutable (which you should already know) which means you cannot change their contents. You can only create new Strings.

Thanks for the solution , finaly it work ^_^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.