我需要能够在链接列表中搜索某些唯一元素(用户名,密码,电子邮件),找到这些元素后,我需要转到列表中的下一个节点并开始一系列允许用户更改的语句档案信息。由于某种原因我的代码不起作用,我不知道如何解决它。任何帮助都会很棒!
GUI的外观
指向帐户类的链接: http://pastebin.com/jnBrcnP1
因此,用户填写所需信息,如果他们想要更改“名称”或“性别”等个人资料信息,他们会更改信息,然后将旁边的ComboBox设置为“是”,然后点击“保存设置”按钮”
以下是链接列表的内容:
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
这是我的按键代码:
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) {
}
}
}
阅读方法:
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();
}
答案 0 :(得分:1)
查看代码,执行此操作:
首先,LinkedList<Account> account = new LinkedList<Account>();
这意味着您每次用户点击保存设置按钮时都会创建列表。
其次,ListIterator<Account> itr = account.listIterator();
,但帐号是一个空列表!因此,您无法将任何单个对象与任何人员数据进行比较。
最后但并非最不重要的是,read(account, "doggydates.txt");
我猜你在完成所有比较后,正在阅读文件数据并填写你的清单。
重新组织您的代码,然后重试。
更新: 在审核完所有代码后,我发现您的设计存在一些问题:
Account
类是您的实体。此类必须包含列表的数据。您可以像LinkedList的Node一样重用此类,指定指向其他Account
对象引用的指针,无需使用ListNode
实例。Account
类。但这是在LinkedList<Account>
内部,因此您有LinkedLists的LinkedList。我猜你不想要这种行为。您可以将设计更改为更简单的设计。我将为您的设计提供2个样品:
表单1.将Acount
类用作LinkedList节点。
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");
}
}
表单2.像实体一样使用Account
类并使用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,我会坚持使用Form 2而不是Form1。希望它可以帮到你。
答案 1 :(得分:0)
我建议你重新考虑你的设计。我看到你正在混合账户类中链接的lsit参数和账户实例的链接列表的概念。
您可能需要的选项: 您有一个链接的帐户列表,对于您的示例,链接列表应该有两个帐户对象。
LinkedList<Account> accounts = new LinkedList<Account>();
load(accounts, file);
应该有两个对象{tobiaccount,peppyaccount},你需要在这里写一个加载函数,这是另一个问题。
然后,每个帐户可能有自己的参数链表,如果这真的是你想要的。
然后你会有链接列表(帐户)的链接列表
{
{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}
}
如果你想这样做,你应该考虑简化你的Account类,并确保其中的列表填充正确。
但,除非您正在做关于链表的奇怪作业,否则这仍然不是一个好的设计。
最佳设计是使用单个链表,而不是嵌套链表;将Accounts放在链接列表上,并通过Account类中已有的set / get方法访问每个参数。