Help Tree Set

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 8
Reputation: ShuiYinDeng is an unknown quantity at this point 
Solved Threads: 0
ShuiYinDeng ShuiYinDeng is offline Offline
Newbie Poster

Help Tree Set

 
0
  #1
Oct 29th, 2009
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???
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
0
  #2
Oct 29th, 2009
Try using trim on the Strings before you add them to the sets.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: ShuiYinDeng is an unknown quantity at this point 
Solved Threads: 0
ShuiYinDeng ShuiYinDeng is offline Offline
Newbie Poster
 
0
  #3
Oct 29th, 2009
Example of Trim is
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
0
  #4
Oct 29th, 2009
to be found in the API docs.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #5
Oct 29th, 2009
The way is use constructor
public TreeSet(Comparator<? super E> comparator)
, and write own String comparator where the case of letter is irrelevant
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
0
  #6
Oct 29th, 2009
Uhm, no. He's using Strings and they are already comparable.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: ShuiYinDeng is an unknown quantity at this point 
Solved Threads: 0
ShuiYinDeng ShuiYinDeng is offline Offline
Newbie Poster
 
0
  #7
Oct 29th, 2009
at where i need use the string trim ?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
0
  #8
Oct 29th, 2009
Between reading the String and adding it to set, of course. Do you need someone to hold your hand when using the bathroom?
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: ShuiYinDeng is an unknown quantity at this point 
Solved Threads: 0
ShuiYinDeng ShuiYinDeng is offline Offline
Newbie Poster
 
0
  #9
Oct 29th, 2009
String Line =oldacc.nextLine();
Line.trim();
oldAcc.add(Line);

iziit somethings like this ?
Last edited by ShuiYinDeng; Oct 29th, 2009 at 4:42 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
0
  #10
Oct 29th, 2009
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC