I need to be able to find and replace user information in a Linked List. I have been reading several tutorials and examples but it doesn’t seem to work. The set method used for linked lists isn’t working. So I am wondering if I am implementing it wrong or what. Any help would be great. Also just FYI my linked list is loaded from a file and basically its just user information with each element on a different line.
i.e.
int index = account.indexOf(hobby);
account.set(index, "New String");
Code:
private void jButtonP1ActionPerformed(java.awt.event.ActionEvent evt) {
LinkedList<Account> account = new LinkedList<Account>();
//user information
String username = jTextFieldP3.getText();
String password = jPasswordFieldP1.getText();
String email = jTextFieldP4.getText();
String name = jTextFieldP1.getText();
String breed = (String) jComboBoxP4.getSelectedItem();
String gender = (String) jComboBoxP3.getSelectedItem();
String age = (String) jComboBoxP1.getSelectedItem();
String state = (String) jComboBoxP2.getSelectedItem();
String hobby = jTextFieldP2.getText();
//combo boxes
String passchange = (String) jComboBoxP13.getSelectedItem();
String emailchange = (String) jComboBoxP14.getSelectedItem();
String namechange = (String) jComboBoxP6.getSelectedItem();
String breedchange = (String) jComboBoxP7.getSelectedItem();
String genderchange = (String) jComboBoxP8.getSelectedItem();
String agechange = (String) jComboBoxP9.getSelectedItem();
String statechange = (String) jComboBoxP10.getSelectedItem();
String hobbychange = (String) jComboBoxP11.getSelectedItem();
String accountcancel = (String) jComboBoxP5.getSelectedItem();
Account a = new Account(username, password, email, name, breed, gender, age, state, hobby);
account.add(a);
if(username.equals("") || password.equals("") || email.equals("")) // If password and username is empty > Do this >>>
{
jButtonP1.setEnabled(false);
jTextFieldP3.setText("");
jPasswordFieldP1.setText("");
jTextFieldP4.setText("");
jButtonP1.setEnabled(true);
this.setVisible(true);
}
else if(a.onList(username) || a.onList(password) || a.onList(email))
{
int index = account.indexOf(hobby);
account.set(index, "New String");
}
else
{
}
}
The problem here si that indexOf() will return -1, because it searches for a String value inside a list with Account values.
You cannot search in the list by its elements’ fields. You must search for that account manually and then set the hobby field: