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: ");
System.out.println();
TreeSet<String> oldAcc = new TreeSet<String>();
Scanner oldacc = new Scanner(new FileReader("OldAccount.txt"));
while (oldacc.hasNext()) {
String Line =oldacc.nextLine();
oldAcc.add(Line);
}
System.out.println(oldAcc);
oldacc.close();

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

}
System.out.println(CurrAcc);
curracc.close();


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

GradAcc.addAll(oldAcc);
GradAcc.removeAll(CurrAcc);
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???

The way is use constructor

public TreeSet(Comparator<? super E> comparator)

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

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();
Line.trim();
oldAcc.add(Line);

iziit somethings like this ?

Edited 7 Years Ago by ShuiYinDeng: n/a

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.

This article has been dead for over six months. Start a new discussion instead.