Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7668631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:24:39+00:00 2026-05-31T15:24:39+00:00

I need to be able to search a Linked List for certain unique elements

  • 0

I need to be able to search a Linked List for certain unique elements (username, password, email) and after finding these elements I need to go to the next node in the list and begin a series of statements allowing users to change profile information. For some reason my code just doesn’t work and I have no idea how to fix it. Any help would be great!

How GUI Looks

GUI

Link to Account Class: http://pastebin.com/jnBrcnP1

So the User fills out the Required information and if they want to change profile information such as “Name” or “Gender” they change the information then set the ComboBox next to it to “Yes” then click the button “Save Settings”.

Here is 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) {
        }

    }
}                                         

Read 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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:24:40+00:00Added an answer on May 31, 2026 at 3:24 pm

    Looking at your code, you do this:

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

    Second, ListIterator<Account> itr = account.listIterator();, but account is an empty list! So you can’t compare any single object with any of your people data.

    Last, but not least, read(account, "doggydates.txt"); I guess you’re reading the file data and filling your list, after all the comparisons have been done.

    Reorganize your code and try again.

    UPDATE:
    After reviewing all your code, I find some issues in your design:

    1. The Account class is your entity. This class must have the data for your list. You can reuse this class like a Node of a LinkedList, specifying the pointer to other Account object reference, no need to use ListNode instance.
    2. To implement a LinkedList, you’re using the Account class. But this is inside a LinkedList<Account>, so you have a LinkedList of LinkedLists. I guess you don’t want this behavior.

    You could change your design to something more simple. I’ll give you 2 samples for your design:

    Form 1. Using your 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 like an entity 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’ll stick to Form 2 instead of Form1. Hope it helps you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to be able to search within a string and find out if
I have a program that I need to be able to search a file
I need to be able to get a list of the groups a user
I work for a medical lab company. They need to be able to search
I need to be able to search for a product based on barcode, keyword
I need to be able to search through the dictionary by zipcode but I
Good afternoon, I need to be able to search a Lucene.Net index with a
I need to be able to search for text in a large number of
I need to be able to Search a directory and its subdirectories for specific
I have a lastname and a firstname. I need to be able to search

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.