954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help Tree Set

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 oldAcc = new TreeSet();
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 CurrAcc = new TreeSet();
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 GradAcc = new TreeSet();

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???

ShuiYinDeng
Newbie Poster
10 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

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

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Example of Trim is

ShuiYinDeng
Newbie Poster
10 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

to be found in the API docs.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

The way is use constructor public TreeSet(Comparator<? super E> comparator) , and write own String comparator where the case of letter is irrelevant

quuba
Posting Pro
573 posts since Nov 2008
Reputation Points: 123
Solved Threads: 106
 

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

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

at where i need use the string trim ?

ShuiYinDeng
Newbie Poster
10 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

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

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

String Line =oldacc.nextLine();
Line.trim();
oldAcc.add(Line);

iziit somethings like this ?

ShuiYinDeng
Newbie Poster
10 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Thanks for the solution , finaly it work ^_^

ShuiYinDeng
Newbie Poster
10 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You