Hi,
How do i write a code that allows a user to enter someone's name and then search for that name from a text file. If the name is not available it opens up a form to register the patient, but if the name is available in the text file, it just updates the profile if necessary
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
class SearchFromFile{
public static void main(String[] args) {
JFrame f=new JFrame();
f.setLayout(null);
final JLabel label=new JLabel("Enter Name to search: ");
final JTextField text=new JTextField(20);
final JButton b=new JButton("Search");
final JLabel lab1=new JLabel("Name: ");
final JTextField text1=new JTextField(20);
final JLabel lab2=new JLabel("Address: ");
final JTextField text2=new JTextField(20);
final JLabel lab3=new JLabel("Contact No: ");
final JTextField text3=new JTextField(20);
final JLabel lab4=new JLabel("Email: ");
final JTextField text4=new JTextField(20);
final JButton button=new JButton("Edit");
final JButton addbutton=new JButton("Add");
final ArrayList list=new ArrayList();
label.setBounds(20,20,150,20);
text.setBounds(170,20,100,20);
lab1.setBounds(20,70,100,20);
text1.setBounds(120,70,100,20);
lab2.setBounds(20,100,100,20);
text2.setBounds(120,100,100,20);
lab3.setBounds(20,130,100,20);
text3.setBounds(120,130,100,20);
lab4.setBounds(20,160,100,20);
text4.setBounds(120,160,100,20);
b.setBounds(170,50,80,20);
addbutton.setBounds(20,190,100,20);
button.setBounds(120, 190,80,20);
lab1.setVisible(false);
text1.setVisible(false);
lab2.setVisible(false);
text2.setVisible(false);
lab3.setVisible(false);
text3.setVisible(false);
lab4.setVisible(false);
text4.setVisible(false);
button.setVisible(false);
addbutton.setVisible(false);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String value = text.getText();
File f = new File("c:/data.txt");
try {
BufferedReader freader = new BufferedReader(new FileReader( f));
String s;
while ((s = freader.readLine()) != null) {
String[] st = s.split(" ");
String name = st[0];
String address = st[1];
String contact = st[2];
String email = st[3];
list.add(name);
if(name.equals(value)) {
int answer = JOptionPane.showConfirmDialog(null, "Do you want to update the record?");
if(answer == JOptionPane.YES_OPTION) {
lab1.setVisible(true);
text1.setVisible(true);
lab2.setVisible(true);
text2.setVisible(true);
lab3.setVisible(true);
text3.setVisible(true);
lab4.setVisible(true);
text4.setVisible(true);
button.setVisible(true);
label.setVisible(false);
text.setVisible(false);
b.setVisible(false);
addbutton.setVisible(false);
text1.setText(name);
text2.setText(address);
text3.setText(contact);
text4.setText(email);
}
}
}
continue....
if(!list.contains(value)) {
int answer = JOptionPane.showConfirmDialog(null, "Record Not Found! Do you want to add the record?");
if(answer == JOptionPane.YES_OPTION) {
lab1.setVisible(true);
text1.setVisible(true);
lab2.setVisible(true);
text2.setVisible(true);
lab3.setVisible(true);
text3.setVisible(true);
lab4.setVisible(true);
text4.setVisible(true);
addbutton.setVisible(true);
label.setVisible(false);
text.setVisible(false);
b.setVisible(false);
button.setVisible(false);
addbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String t1 = text1.getText();
String t2 = text2.getText();
String t3 = text3.getText();
String t4 = text4.getText();
File f3 = new File("c:/data.txt");
try {
BufferedWriter output = new BufferedWriter(new FileWriter(f3,true));
output.newLine();
output.write(t1 + " " + t2 + " " + t3 + " " + t4);
output.close();
JOptionPane.showMessageDialog(null,"Record is added successfully!");
}
catch(Exception e){}
}
});
}
}
freader.close();
}
catch (Exception e) { }
}
});
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String value = text.getText();
String t1 = text1.getText();
String t2 = text2.getText();
String t3 = text3.getText();
String t4 = text4.getText();
File f3 = new File("c:/new.txt");
File f4 = new File("c:/data.txt");
try {
BufferedWriter output = new BufferedWriter(new FileWriter(f3));
BufferedReader freader = new BufferedReader(new FileReader(f4));
String s;
while ((s = freader.readLine()) != null) {
String[] f = s.split(" ");
String n = f[0];
String add = f[1];
String con= f[2];
String emailid = f[3];
if (!n.equals(value)) {
output.write(s);
output.newLine();
}
}
freader.close();
output.write(t1 + " " + t2 + " " + t3 + " " + t4);
output.close();
JOptionPane.showMessageDialog(null,"Record is updated successfully!");
}
catch (Exception e) {}
f4.delete();
f3.renameTo(f4);
}
});
f.add(label);
f.add(text);
f.add(lab1);
f.add(text1);
f.add(lab2);
f.add(text2);
f.add(lab3);
f.add(text3);
f.add(lab4);
f.add(text4);
f.add(b);
f.add(button);
f.add(addbutton);
f.setSize(400,300);
f.setVisible(true);
}
}