【发布时间】:2012-03-14 06:53:37
【问题描述】:
我需要能够在链接列表中搜索某些唯一元素(用户名、密码、电子邮件),在找到这些元素后,我需要转到列表中的下一个节点并开始一系列允许用户更改的语句档案信息。出于某种原因,我的代码不起作用,我不知道如何修复它。任何帮助都会很棒!
GUI 的外观
帐号类链接: http://pastebin.com/jnBrcnP1
所以用户填写必填信息,如果他们想更改个人资料信息,例如“姓名”或“性别”,他们会更改信息,然后将旁边的组合框设置为“是”,然后单击按钮“保存设置” ”。
链接列表如下所示:
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();
}
【问题讨论】:
-
您为什么要两次搜索同一个帐户?您已经通过迭代器获得了正确的对象。
-
您的评论显示“如果密码 AND 用户名为空 > 执行此操作”,但您的代码显示“如果密码 OR 用户名 OR 电子邮件为空..."
-
我可能有误会,但这里的列表似乎不是正确的结构......也许是地图?
标签: java linked-list