Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing elements in one hash set to the elements in another

 
Fazz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The following code was repeated three times to store 3 files into 3
individual hash sets. I now need to compare file A to file B, i want to
see how many of the words in set A are also on set B. (Each set
contains the same number of words). Im not sure how to implement this,
can anyone help??



public FileIn() {


try {
// make sure there is a text file in the java directory where the java
// code is saved
in = new BufferedReader(new FileReader("C:\\Program
Files\\Java\\example.txt"));
set = new HashSet();


int Len = 1;
while(Len>0) {
String line = in.readLine();
try {
Len = line.length();
System.out.println(line);
set.add(line);


} catch(NullPointerException npe){


Len = 0; //no more file to read


I think using the retainAll method might be an idea:
HashSet ab = (HashSet)a.clone();
ab.retainAll(b);


...but im not sure how to incorporate this into my program?


Farah
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Farah Omarshah: I think using the retainAll method might be an idea:
HashSet ab = (HashSet)a.clone();
ab.retainAll(b);


...but im not sure how to incorporate this into my program?



What is casusing you concerns about incorporating these two lines into your code?
 
Fazz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i meant was, is this all the code i need? is anything missing??
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the code do what you want it to do ? If so then there is nothing missing. If not then there is something missing. You will then need to tell us what it is doing and what you were expecting it to do.
 
Fazz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well the code compiles but how can i check that it works??
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to create a few test cases where you know what the output should be and use them to test your code.
 
Fazz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would a simple if statement suffice for a test case?
A n if statement to check that oth sets contain the same elements followed by a 'system.out.println' to show the output??


Farah
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way to write test cases is so that they are

- repeatable and
- fully automated

That is, you should be able to start your tests with the press of a single button (for example), and get a result such as "all tests passed" or "2 failures". Only if you have failures, you should need to take a closer look at how the actual results differed from the expected.

The most popular library that helps in doing such tests - in fact I'd call it the de facto standard for java - is JUnit: http://www.junit.org/

If you'd like to discuss this in more detail, our Testing forum probably would be the best place.

Hope this helps...
 
Fazz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have decided to use the retainAll method from the java.util library.
The following presents my code, it wont compile, im not sure what ive done wrong. can anyone help please??

import java.io.*;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.StringTokenizer;


public class File {
// assigns BufferedReader an instance name to be used in the code
BufferedReader in;
BufferedReader inOne;
BufferedReader inTwo;
HashSet a;
HashSet b;
HashSet c;


public File() {

try {
// make sure there is a text file in the java directory where the java
// code is saved
in = new BufferedReader(new FileReader("U:\\example.txt"));
inOne = new BufferedReader(new FileReader("U:\\exampleOne.txt"));
inTwo = new BufferedReader(new FileReader("U:\\exampleTwo.txt"));
a = new HashSet();
b = new HashSet();
c = new HashSet();


int Len = 1;
while(Len>0) {
String line = in.readLine();
try {
Len = line.length();
System.out.println(line);
a.add(line);
} catch(NullPointerException npe){
Len = 0; //no more file to read
}
}

int LenOne = 1;
while(LenOne>0) {
String line = inOne.readLine();
try {
LenOne = line.length();
System.out.println(line);
b.add(line);
} catch(NullPointerException npe){
LenOne = 0; //no more file to read
}
}

int LenTwo = 1;
while(LenTwo>0) {
String line = inTwo.readLine();
try {
LenTwo = line.length();
System.out.println(line);
c.add(line);
} catch(NullPointerException npe){
LenTwo = 0; //no more file to read
}
}
private static void findDupsByRetainAll()
{

HashSet<String> dups = new HashSet<String>( a );


dups.retainAll( b );

for ( String a : dups )
{
System.out.println( "dup found by retainAll: " + a );
}
}

in.close();

// if any error occured during the try statement the system will display
// the error that it encountered
}
catch(IOException ioe) {
System.out.println(ioe.toString());
}
}


public static void main(String args[]) {
//this gets executed when the java file is run
//this then (starting from the top) does what u tell it
File newFile = new File();
findDupsByRetainAll();
// newFileIn().go;

}
}
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Farah Omarshah:
I have decided to use the retainAll method from the java.util library.
The following presents my code, it wont compile, im not sure what ive done wrong. can anyone help please??

Did you ever mention the funny output when the compiler refuses to finish without problems?

These messages contain:
- A line number, where the compiler encountered an error. Sometimes the correction has to be done at a different location, but most times the line is right or at least very close.
- A message, what the compiler is claiming about.

When you post to a forum, cite the errormessage, and mark the line, because the we can find the error very fast.
Else we have to compile the code ourself, or check every single line.

And please get used to the code-tags!
[ April 02, 2006: Message edited by: Stefan Wagner ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also calling your class File is likely to be confusing since there is a standard library class java.io.File.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic