文本框中的数组

时间:2015-06-20 21:10:01

标签: java

我正在创建一个用户密码数据库写入文件。我尝试过使用字符串数组用户名,但它仍然像常规字符串一样渲染;输出打印您在文件第二行中输入的最后一行(第一行是用户名,电子邮件...)

btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
    openFile();
    addRecords();
    closeFile();
}

public void addRecords(){
    int records = Integer.parseInt(textField_4.getText());
    x.format("%-25s %-25s %-25s %-25s \n", "Username", "Email", "Mojang Password", "Email Password");
    String username[] = new String[records];
    for(int counter = 0; counter<records; counter++){
        username[counter] = textField.getText();
        textField.setText("");
        String email = textField_1.getText();
        String userpass = textField_2.getText();
        String emailpass = textField_3.getText();
        x.format("%-25s %-25s %-25s %-25s \n", username[counter], email, userpass, emailpass);  
    }

1 个答案:

答案 0 :(得分:1)

您需要更改存储数据的方式。每条记录都应该是这样的

record[0] - {username,email,userpass,emailpass} -  for user 1
record[1] - {username,email,userpass,emailpass} -  for user 2

将其保存为

for user1 - 
record[0][0] = username
record[0][1] = email 
record[0][2] = userpass 
record[0][3] = emailpass

for user2 - 

record[1][0] = username
record[1][1] = email 
record[1][2] = userpass 
record[1][3] = emailpass

当你访问它时

for(int counter = 0; counter<records; counter++){
        username = record[counter][0];
        String email = record[counter][1];
        String userpass = record[counter][2];
        String emailpass = record[counter][3];
        x.format("%-25s %-25s %-25s %-25s \n", username, email, userpass, emailpass);  
    }