943,985 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 562
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 29th, 2009
0

Help Tree Set

Expand Post »
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???
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShuiYinDeng is offline Offline
10 posts
since Oct 2009
Oct 29th, 2009
0
Re: Help Tree Set
Try using trim on the Strings before you add them to the sets.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 29th, 2009
0
Re: Help Tree Set
Example of Trim is
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShuiYinDeng is offline Offline
10 posts
since Oct 2009
Oct 29th, 2009
0
Re: Help Tree Set
to be found in the API docs.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 29th, 2009
0
Re: Help Tree Set
The way is use constructor
Quote ...
public TreeSet(Comparator<? super E> comparator)
, and write own String comparator where the case of letter is irrelevant
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Oct 29th, 2009
0
Re: Help Tree Set
Uhm, no. He's using Strings and they are already comparable.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 29th, 2009
0
Re: Help Tree Set
at where i need use the string trim ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShuiYinDeng is offline Offline
10 posts
since Oct 2009
Oct 29th, 2009
0
Re: Help Tree Set
Between reading the String and adding it to set, of course. Do you need someone to hold your hand when using the bathroom?
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 29th, 2009
0
Re: Help Tree Set
String Line =oldacc.nextLine();
Line.trim();
oldAcc.add(Line);

iziit somethings like this ?
Last edited by ShuiYinDeng; Oct 29th, 2009 at 4:42 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShuiYinDeng is offline Offline
10 posts
since Oct 2009
Oct 29th, 2009
0
Re: Help Tree Set
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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How can I output program lines?
Next Thread in Java Forum Timeline: maven...........mvn deploy





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC