问题:
将信息存储在Account对象的ArrayList中。
*注意:对于Netbeans用户,该文件必须放在测试文件夹中,并使用new File("test/pw.txt");
您的程序将提示用户输入其电子邮件地址。
示例输出:
Enter your email address (q to quit):
draco@hogwarts.com
Email not found, please try again (q to quit):
dmalfoy@hogwarts.com
Email not found, please try again (q to quit):
q
Goodbye!
pw.txt
Hagrid hagrid@hogwarts.com 111
Harry harry@hogwarts.com killvoldy777
Ron ron@hogwarts.com mypassword123
Hermione hermione@hogwarts.com 98fJG83h*4iwrej!
下一步,我应该做什么,第16行是异常
import java.util.*;
import java.io.*;
public class LoginSim {
private static int index;
public static void main(String args[]) throws
FileNotFoundException, ArrayIndexOutOfBoundsException {
String em;
String pw;
Scanner f = new Scanner(new File("src/pw.txt"));
Scanner kb = new Scanner(System.in);
String[] email = new String[3];
String[] password = new String[3];
int i = 0;
while (f.hasNext()) {
email[i] = String.valueOf(f.hasNext());
password[i] = String.valueOf(f.hasNext());
i++;
}
System.out.println("Enter Email:");
em = kb.next();
System.out.println("Enter Password:");
pw = kb.next();
if (index != -1) {
System.out.println("Enter pw:");
pw = kb.next();
int tries = 0;
while (!pw.equals(tries < 2) &&
!pw.equals(password[index])) {
System.out.println("Incorrect Password, Try Again");
tries++;
pw = kb.next();
}
if (pw.equals(password[index])) {
System.out.println("Successful Login");
}else {
System.out.println("3 Strikes, Locked out");
}
System.out.println("Email not found");
}
}
}
答案 0 :(得分:0)
好吧,您在这一行java.lang.ArrayIndexOutOfBoundsException: 3
中得到了email[i] = String.valueOf(f.hasNext());
,因为数组的大小为3(String[] email = new String[3];
),但是您试图向其中添加3个以上的条目,根据样本“ pw.txt”至少4个。
另外,您的password
数组也有同样的问题。