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

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();
}
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:
Accountclass 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 otherAccountobject reference, no need to useListNodeinstance.Accountclass. But this is inside aLinkedList<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
Acountclass as a LinkedList node.Form 2. Use the
Accountclass like an entity and use the Java LinkedList implementation:IMO, I’ll stick to Form 2 instead of Form1. Hope it helps you.