In a nutshell, my program allows the user to enter member details, which are then written to a text file on a new line. I am having trouble with the search. What I have to do is when a user types in a member ID in the text field, it looks through the text file and if that member ID is present, return the whole line.
This is my code (I have tried lots but don't know what to do under searchBtn:
/**
* Creates new form Members
*/
public Members() {
initComponents();
}
String[][] membersArray = new String[3][5];
String lineRead;
int i = 0;
int j;
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {
saveBtn.setEnabled(false);
addBtn.setEnabled(true);
try {
FileWriter writer = new FileWriter("memberDetails.txt", true);
writer.write(idTxt.getText() + "," + nameTxt.getText() + "," + addressTxt.getText() + "," + phoneTxt.getText() + "," + birthTxt.getText() + "\n");
writer.close();
}
catch (IOException e){
}
}
private void addBtnActionPerformed(java.awt.event.ActionEvent evt) {
saveBtn.setEnabled(true);
addBtn.setEnabled(false);
idTxt.setText("");
nameTxt.setText("");
addressTxt.setText("");
phoneTxt.setText("");
birthTxt.setText("");
}
private void searchBtnActionPerformed(java.awt.event.ActionEvent evt) {
try {
FileReader reader = new FileReader("memberDetails.txt");
BufferedReader buffer = new BufferedReader(reader);
while((lineRead = buffer.readLine()) !=null) {
for (i = 0; i < 2; i ++) {
for (j = 0; j < 5; j++) {
membersArray[i][j] = String.valueOf(lineRead.split(","));
}
}
}
if (searchTxt.getText() == membersArray[i][0]) {
// Print whole line
}
reader.close();
buffer.close();
}
catch (IOException e) {
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Members.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Members.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Members.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Members.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Members().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton addBtn;
private javax.swing.JLabel addressLbl;
private javax.swing.JTextField addressTxt;
private javax.swing.JLabel birthLbl;
private javax.swing.JTextField birthTxt;
private javax.swing.JLabel idLbl;
private javax.swing.JTextField idTxt;
private javax.swing.JLabel nameLbl;
private javax.swing.JTextField nameTxt;
private javax.swing.JLabel phoneLbl;
private javax.swing.JTextField phoneTxt;
private javax.swing.JButton saveBtn;
private javax.swing.JButton searchBtn;
private javax.swing.JTextField searchTxt;
// End of variables declaration
}