Java – find and replace elements in a linked list
I need to be able to search for some unique elements (user name, password, email) in the link list. After finding these elements, I need to go to the next node in the list and start a series of statements to allow users to change profile information For some reason my code doesn't work. I don't know how to solve it Any help would be great!
GUI appearance
Link to account class: http://pastebin.com/jnBrcnP1
Therefore, users fill in the required information. If they want to change personal information such as "name" or "gender", they will change the information and then Combo@R_9_2419 @Set to yes, and then click the Save Settings button
The following is how the linked list looks:
tobi tobi123 tobi@hotmail.com tobi Mixed Breed Male 1-2 Virginia Walking peppy peppy123 peppy@hotmail.com peppy Chihuahua Male 5-6 Virginia Eating
This is my button code:
private void jButtonP1ActionPerformed(java.awt.event.ActionEvent evt) { //New Linked List created from file LinkedList<Account> account = new LinkedList<Account>(); try { read(account,"doggydates.txt"); } catch (Exception e) { System.err.println(e.toString()); } display(account); //user information String username = jTextFieldP3.getText(); String password = jPasswordFieldP1.getText(); String email = jTextFieldP4.getText(); String name = jTextFieldP1.getText(); String breed = (String) jCombo@R_9_2419@P4.getSelectedItem(); String gender = (String) jCombo@R_9_2419@P3.getSelectedItem(); String age = (String) jCombo@R_9_2419@P1.getSelectedItem(); String state = (String) jCombo@R_9_2419@P2.getSelectedItem(); String hobby = jTextFieldP2.getText(); //change combo @R_9_2419@es String passchange = (String) jCombo@R_9_2419@P13.getSelectedItem(); String emailchange = (String) jCombo@R_9_2419@P14.getSelectedItem(); String namechange = (String) jCombo@R_9_2419@P6.getSelectedItem(); String breedchange = (String) jCombo@R_9_2419@P7.getSelectedItem(); String genderchange = (String) jCombo@R_9_2419@P8.getSelectedItem(); String agechange = (String) jCombo@R_9_2419@P9.getSelectedItem(); String statechange = (String) jCombo@R_9_2419@P10.getSelectedItem(); String hobbychange = (String) jCombo@R_9_2419@P11.getSelectedItem(); //cancel combo @R_9_2419@ String accountcancel = (String) jCombo@R_9_2419@P5.getSelectedItem(); 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 { ListIterator<Account> itr = account.listIterator(); while (itr.hasNext()) { Account item = itr.next(); if(item.getUsername().equals(username) && item.getpassword().equals(password)) { if(passchange.equals("Yes")) { for(Account acc : account){ if(acc.getUsername().equals(username)){ acc.goToNext(); acc.setDataAtCurrent(password); } } } if(emailchange.equals("Yes")) { for(Account acc : account){ if(acc.getUsername().equals(username)){ acc.goToNext(); acc.goToNext(); acc.setDataAtCurrent(email); } } } if(namechange.equals("Yes")) { for(Account acc : account){ if(acc.getUsername().equals(username)){ acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.setDataAtCurrent(name); } } } if(breedchange.equals("Yes")) { for(Account acc : account){ if(acc.getUsername().equals(username)){ acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.setDataAtCurrent(breed); } } } if(genderchange.equals("Yes")) { for(Account acc : account){ if(acc.getUsername().equals(username)){ acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.setDataAtCurrent(gender); } } } if(agechange.equals("Yes")) { for(Account acc : account){ if(acc.getUsername().equals(username)){ acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.setDataAtCurrent(age); } } } if(statechange.equals("Yes")) { for(Account acc : account){ if(acc.getUsername().equals(username)){ acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.setDataAtCurrent(state); } } } if(hobbychange.equals("Yes")) { for(Account acc : account){ if(acc.getUsername().equals(username)){ acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.setDataAtCurrent(hobby); } } } if(accountcancel.equals("Yes")) { for(Account acc : account){ if(acc.getUsername().equals(username)){ acc.deleteCurrentNode(); acc.goToNext(); acc.deleteCurrentNode(); acc.goToNext(); acc.deleteCurrentNode(); acc.goToNext(); acc.deleteCurrentNode(); acc.goToNext(); acc.deleteCurrentNode(); acc.goToNext(); acc.deleteCurrentNode(); acc.goToNext(); acc.deleteCurrentNode(); acc.goToNext(); acc.deleteCurrentNode(); acc.goToNext(); acc.deleteCurrentNode(); } } } } } String file_name = "doggydates.txt"; try { FileWriter fstream = new FileWriter(file_name); BufferedWriter out = new BufferedWriter(fstream); ListIterator itr2 = account.listIterator(); while (itr2.hasNext()) { Account element = (Account) itr2.next(); out.write("" + element); out.newLine(); } out.close(); System.out.println("File created successfully."); } catch (Exception e) { } } }
Reading method:
public static void read(LinkedList<Account> account,String inputFileName) throws java.io.IOException{ BufferedReader infile = new BufferedReader(new FileReader(inputFileName)); while(infile.ready()) { String username = readLine(infile); String password = readLine(infile); String email = readLine(infile); String name = readLine(infile); String breed = readLine(infile); String gender = readLine(infile); String age = readLine(infile); String state = readLine(infile); String hobby = readLine(infile); Account a = new Account(username,password,email,name,breed,gender,age,state,hobby); account.add(a); a.showList(); } infile.close(); }
Solution
View the code and do the following:
First, LinkedList < account > account = new LinkedList < account > (); This means that the list is created each time the user clicks the Save Settings button
2、 Listiterator < account > ITR = account listIterator();, But the account number is an empty list! Therefore, you cannot compare any single object with any person data
Last but not least, read (account, "doggydates. TXT"); After all the comparisons, I guess you're reading the file data and filling out the list
Reorganize your code and try again
Update: After reviewing all the codes, I found some problems in your design:
>The account class is your entity This class must contain data from the list You can reuse this class like the node of LinkedList and specify pointers to other account object references without using the listnode instance. > To implement LinkedList, you use the account class But this is inside the LinkedList < account >, so you have the LinkedList of linkedlists I guess you don't want this behavior
You can change the design to a simpler design I will give you 2 samples for your design:
Form 1 Use your acount class as the LinkedList node
public class Account { private Account next; private Account prevIoUs; private String username; private String password; private String email; private String name; private String breed; private String gender; private String age; private String state; private String hobby; //constructor logic... //getters and setters... //toString method (for System.out.println) } public class AccountLinkedList { private Account head; private int size; public AccountLinkedList() { this.size = 0; this.head = null; } public boolean insert(Account account) { if (this.head == null) { this.head = account; } else { Account current = head; while (current.getNext() != null) { current = current.getNext(); } //maintain the LinkedList order current.setNext(account); account.setPrevIoUs(current); } } public Account findAccount(Account account) { Account current = head; while (current != null) { if (current.equals(account) { return current; } current = current.getNext(); } return null; } //create the other methods to search,delete and traverse your list... } public class MyProgram { public void jButtonP1ActionPerformed(java.awt.event.ActionEvent evt) { Account account = new Account(); //set the account data,I'll just stick to username and //password attributes String username = jTextFieldP3.getText(); String password = jPasswordFieldP1.getText(); account.setUsername(username); account.setPassword(password); //perform the update updateData(); } public void updateData(Account account) { AccountLinkedList accountList = new AccountLinkedList; //loading data into our list loadDataFromFile(accountList,"myFile.txt"); //perform the search operation Account accountAux = accountList.findAccount(account); //if the account is found,then update the accountAux data if (accountAux != null) { updateAccountData(accountAux,account); } else { accountList.insert(account); } //saving the modified data saveDataToFile(accountList,"myFile.txt"); } }
Form 2 Use the account class like an entity and implement it using java LinkedList:
//check that Now you don't have the next and prevIoUs attributes public class Account { private String username; private String password; private String email; private String name; private String breed; private String gender; private String age; private String state; private String hobby; //constructor logic... //getters and setters... //toString method (for System.out.println) } public class MyProgram { public void jButtonP1ActionPerformed(java.awt.event.ActionEvent evt) { Account account = new Account(); //set the account data,I'll just stick to username and //password attributes String username = jTextFieldP3.getText(); String password = jPasswordFieldP1.getText(); account.setUsername(username); account.setPassword(password); //perform the update updateData(); } public void updateData(Account account) { LinkedList<Account> accountList = new LinkedList<Account>(); //loading data into our list loadDataFromFile(accountList,"myFile.txt"); //perform the search operation ListIterator<Account> li = accountList.listIterator(); Account accountAux = null; while(li.hasNext()) { accountAux = li.next(); //matching the account data outside with the current account //of the list iterator,this is just a sample,you can change //this logic if (accountAux.equals(account) { updateAccountData(accountAux,account); break; } accountAux = null; } //in case the account was not fount in the LinkedList,add it. if (accountAux == null) accountList.add(account); //saving the modified data saveDataToFile(accountList,"myFile.txt"); } }
IMO, I will insist on using form 2 instead of form 1 I hope it can help you