Find and replace items in a linked list

I need to be able to search the Linked List for specific unique elements (username, password, email address), and after finding these elements, I need to go to the next node in the list and start a series of statements that allow users to change profile information. For some reason, my code just doesn't work, and I don't know how to fix it. Any help would be great!

What the GUI looks like

GUI

Link to account class: http://pastebin.com/jnBrcnP1

Thus, the User fills in the required information, and if they want to change the profile information, such as "Name" or "Gender", they change the information and then set the ComboBox next to it "Yes", then click the "Save Settings" button "

Here's what the linked list looks like:

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 

Here 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) jComboBoxP4.getSelectedItem(); String gender = (String) jComboBoxP3.getSelectedItem(); String age = (String) jComboBoxP1.getSelectedItem(); String state = (String) jComboBoxP2.getSelectedItem(); String hobby = jTextFieldP2.getText(); //change 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(); //cancel combo box String accountcancel = (String) jComboBoxP5.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(); } 
+4
source share
2 answers

Looking at your code, you do this:

First, LinkedList<Account> account = new LinkedList<Account>(); this means that you create a list every time the user clicks the Save Settings button.

Secondly, ListIterator<Account> itr = account.listIterator(); but the account is an empty list! Thus, you cannot compare any object with the data of your people.

And last but not least: read(account, "doggydates.txt"); I think you are reading the file data and filling out your list after all comparisons.

Recover your code and try again.

UPDATE: After reviewing all your code, I found some problems in your design:

  • The Account class is your entity. This class must have data for your list. You can reuse this class as a Node LinkedList by pointing to another link to an Account object, you do not need to use an instance of ListNode .
  • To implement LinkedList, you use the Account class. But this is inside LinkedList<Account> , so you have LinkedList LinkedLists. I think you do not want this behavior.

You can change your design to something simpler. I will give you 2 samples for your design:

Form 1. Using the Acount class as a 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 as an object and use the Java LinkedList implementation:

 //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 stick to form 2 instead of Form1. Hope this helps you.

+1
source

I advise you to reconsider your design. I see that you are mixing the concept of lsit related parameters in the Account class and related account instances.

Possible option: You have a linked list of accounts, for your example, a linked list should have two account objects.

 LinkedList<Account> accounts = new LinkedList<Account>(); load(accounts, file); 

which should have two objects {tobiaccount, peppyaccount}, you need to write the download function here, this is another question.

Then each account can have its own linked list for its parameters, if this is really what you want.

Then you will have a list of links link lists (accounts)

 { {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} } 

If you want to go this route, you should consider simplifying the class of your account and make sure that the list is filled out correctly in it.

HOWEVER , this is not a very good design so far unless you are doing weird homework about linked lists.

An optimal design will use one linked list, not a nested one; placing accounts in a linked list and accessing each parameter using the set / get methods that you already have in the Account class.

0
source

Source: https://habr.com/ru/post/1401312/


All Articles