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 whether a word exist in a file or not?

Description

The code given below can be used to find whether a word exists in a file or not. Any file can be selected via JFileChooser and any input word can be given as input to search via InputDialog object. JFileChooser, FileReader, StrignTokenizer are the major objects involved in this code. Input word is matched with each token and result about word existence is displayed in message dialogue.

Sample Code
  • Filename:FindWordExistence .java

import java.sql.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class FindWordExistence {
JPanel panel;
JFrame jf;
JLabel label1,label2;
JButton Browse,Check;
JTextField textfield1,textfield2,textfield3;
JPasswordField passwordfield;
JTextArea textArea1;
String s="";
public FindWordExistence() {
initComponents();
handlingEvents();
}
public void initComponents() {
jf=new javax.swing.JFrame();
jf.setTitle("FindWordExistence");
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("Choose File");
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);
Check=new javax.swing.JButton("Check");
Check.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
Check.setBounds(250,350,140,30);
jf.add(Check);
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,120,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) {
String inputFile=fc.getSelectedFile().getPath();
String filename=fc.getSelectedFile().getName();
textfield1.setText(inputFile);
String st1="";
try {
FileReader f = new FileReader(inputFile);
BufferedReader br = new BufferedReader(f);
while((st1 = br.readLine()) != null) {
s = s + st1;
}
f.close();
textArea1.setText(s);
}
catch (IOException e) {
System.out.println(e);
}
}
}
});
Check.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
String inputWord=JOptionPane.showInputDialog(null,"Enter Word");
textArea1.setText("Words in the File"+"\n");
int i = 0;
StringTokenizer st = new StringTokenizer(s);
i = st.countTokens();
boolean exist=false;
while (st.hasMoreTokens()) {
String token = st.nextToken();
if(inputWord.equals(token)) {
exist=true;
}
textArea1.append(token+"\n");
}
textArea1.append("Total Words in the File="+i);
if(exist) {
JOptionPane.showMessageDialog(null,"Word exists in the file");
} else {
JOptionPane.showMessageDialog(null,"Word does not exist in the file");
}

}
});
}
public static void main(String args[]) {
FindWordExistence fwe = new FindWordExistence();
}
}

Screenshots

Find whether a word exist in a file or not
Any file can be selected via JFileChooser and any input word can be given as input to search via InputDialog object
JFileChooser, FileReader, StrignTokenizer are the major objects involved in this code
Input word is matched with each token and result about word existence is displayed in message dialogue
Whether to finding a word exist in a file or not