Amazing technological breakthrough possible @S-Logix pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

How to find common terms between two files in java?

Description

This program demonstrates how to select the common terms between two files. Two files can be selected. File is read and content is split into array of words. Both array is compared and common term is added into List. Using Iterator object elements in the List object is accessed and displayed in JTextArea.

Sample Code
  • Filename: CommonTerm.java

import java.sql.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class CommonTerm {
JPanel panel;
JFrame jf;
JLabel label1,label2;
JButton Browse,Target,Terms;
JTextField textfield1,textfield2,textfield3;
JPasswordField passwordfield;
String inputFile,inputFile2;
String filename,filename2;
String s="";
String s2="";
String [] terms1,terms2;
Set<String> commonTerms = new HashSet<String>();
JTextArea textArea1;
public CommonTerm() {
initComponents();
handlingEvents();
}
public void initComponents() {
jf=new javax.swing.JFrame();
jf.setTitle("CommonTerm");
jf.setLayout(null);
jf.setSize(800,500);
jf.show();
jf.setVisible(true);

JScrollPane scrollBar=new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jf.add(scrollBar);
label1=new javax.swing.JLabel("File1");
label1.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
label1.setBounds(50,80,200,40);
jf.add(label1);
textfield1=new javax.swing.JTextField();
textfield1.setFont(new Font("Monotype Corsiva", Font.BOLD, 18));
textfield1.setBounds(250,80,200,30);
jf.add(textfield1);
Browse=new javax.swing.JButton("Browse");
Browse.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
Browse.setBounds(480,80,140,30);
jf.add(Browse);

label1=new javax.swing.JLabel("File2");
label1.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
label1.setBounds(50,130,200,40);
jf.add(label1);
textfield2=new javax.swing.JTextField();
textfield2.setFont(new Font("Monotype Corsiva", Font.BOLD, 18));
textfield2.setBounds(250,130,200,30);
jf.add(textfield2);
Target=new javax.swing.JButton("Browse");
Target.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
Target.setBounds(480,130,140,30);
jf.add(Target);
Terms=new javax.swing.JButton("Common Terms");
Terms.setFont(new Font("Monotype Corsiva", Font.BOLD, 20));
Terms.setBounds(250,170,180,30);
jf.add(Terms);
textArea1=new javax.swing.JTextArea();
textArea1.setFont(new Font("Monotype Corsiva", Font.BOLD, 18));
textArea1.setLineWrap(true);
textArea1.setWrapStyleWord(true);
JScrollPane scrollBar1=new JScrollPane(textArea1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollBar1.setBounds(250,220,200,200);
jf.add(scrollBar1);

}

public void handlingEvents() {
Browse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (fc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
inputFile=fc.getSelectedFile().getPath();
filename=fc.getSelectedFile().getName();
textfield1.setText(inputFile);
}
}
});

Target.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (fc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
inputFile2=fc.getSelectedFile().getPath();
filename2=fc.getSelectedFile().getName();
textfield2.setText(inputFile2);
}

}
});
Terms.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
readFile1();
readFile2();
split1();
split2();
findCommon();
showResult();
}
});

}

public void readFile1() {

String st1="";
try {
FileReader f = new FileReader(inputFile);
BufferedReader br = new BufferedReader(f);
while((st1 = br.readLine()) != null) {
s = s + st1;

}
s=s.trim();
f.close();

}
catch (IOException e) {
System.out.println(e);
}

}

public void readFile2() {

String st1="";
try {
FileReader f = new FileReader(inputFile2);
BufferedReader br = new BufferedReader(f);
while((st1 = br.readLine()) != null) {
s2 = s2 + st1;

}
s2=s2.trim();
f.close();

}
catch (IOException e) {
System.out.println(e);
}

}

public void split1() {
terms1 = s.split(" ");
for(String t1:terms1) {
System.out.println(t1);
}

}
public void split2() {
terms2 = s2.split(" ");
for(String t2:terms2) {
System.out.println(t2);
}

}
public void findCommon() {
for(String t1:terms1) {
for(String t2:terms2) {
if (t1.equals(t2)) {
commonTerms.add(t2);
}
}
}
}
public void showResult() {
Iterator i = commonTerms.iterator();
while(i.hasNext()) {
String word = (String)i.next();
textArea1.append(word);
textArea1.append("\n");
}
}
public static void main(String args[]) {
CommonTerm ct = new CommonTerm();
}
}

Screenshots

Find common terms between two files in java