I have a program which allows the user to search a record from a file by typing the name. Then the record will appear in the text boxes provided, so that the user can edit them. However, i can read the file, but i dont know how to edit or replace the line after the user edits the textboxes.
I would appreciate any help.
This is the part which needs to be fixed.
I would appreciate any help.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Vector;
public class Replace {
JFrame frame = new JFrame("Replace");
JPanel panel = new JPanel();
JButton search = new JButton("Search");
JButton update = new JButton("Update");
JTextField textfield1 = new JTextField(15);
JTextField textfield2 = new JTextField(15);
JTextField textfield3 = new JTextField(15);
JTextField textfield4 = new JTextField(15);
JTextField textfield5 = new JTextField(15);
void showFrame(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setLayout(null);
search.setBounds(10, 15, 133, 20);
textfield1.setBounds(150, 15, 124, 20);
textfield2.setBounds(150, 50, 124, 20);
textfield3.setBounds(150, 70, 124, 20);
textfield4.setBounds(150, 90, 124, 20);
textfield5.setBounds(150, 110, 124, 20);
panel.add(textfield1);
panel.add(textfield2);
panel.add(textfield3);
panel.add(textfield4);
panel.add(textfield5);
panel.add(search);
panel.add(update);
frame.add(panel);
frame.setSize(290,210);
frame.setVisible(true);
//search by name
search.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try {
String stringSearch = textfield1.getText();
BufferedReader bf = new BufferedReader(new FileReader("c:\\file.txt"));
int linecount = 0;
String line;
Vector myVector = new Vector();
while (( line = bf.readLine()) != null){
myVector.add(line);
linecount++;
int indexfound = line.indexOf(stringSearch);
if (indexfound > -1) {
String[] word = line.split("\t");
String firstword = word[1];
String secondword = word[2];
String thirdword = word[3];
textfield1.setText(stringSearch);
textfield2.setText(stringSearch);
textfield3.setText(firstword);
textfield4.setText(secondword);
textfield5.setText(thirdword);
}
}
bf.close();
}
catch (IOException e) {
System.out.println("IO Error Occurred: " + e.toString());
}
}
});
//update
update.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try {
File thefile = new File("c:\\file.txt");
BufferedReader bf = new BufferedReader(new FileReader(thefile));
String replace = "", oldtext = "";
while((replace = bf.readLine()) != null){
oldtext += replace + "\r\n";
}
bf.close();
String a = textfield1.getText();
String b = textfield1.getText();
String newtext = oldtext.replaceAll(a, B)/>/>/>/>/>/>;
FileWriter writer = new FileWriter("c:\\file.txt");
writer.write(newtext);
writer.close();
}
catch (IOException e) {
System.out.println("IO Error Occurred: " + e.toString());
}
}
});
}
public static void main( String [] args ) {
Replace Replace = new Replace();
Replace.showFrame();
}
}
This is the part which needs to be fixed.
update.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try {
File thefile = new File("c:\\file.txt");
BufferedReader bf = new BufferedReader(new FileReader(thefile));
String replace = "", oldtext = "";
while((replace = bf.readLine()) != null){
oldtext += replace + "\r\n";
}
bf.close();
String a = textfield1.getText();
String b = textfield1.getText();
String newtext = oldtext.replaceAll(a, B)/>/>/>/>/>/>;
FileWriter writer = new FileWriter("c:\\file.txt");
writer.write(newtext);
writer.close();
}
catch (IOException e) {
System.out.println("IO Error Occurred: " + e.toString());
}
}
});