For my computer science class I'm making an website username/password program. I decided to use a 2D string array, and it hasn't been working out the best. I tried to make a file reader to read the logins that get written but I keep getting the ArrayIndexOutOfBoundsException error. My file reader code is below, and also included is my login input code. I am just starting Java so I have very basic programming knowledge.
private void fileReader() throws FileNotFoundException {
File inFile = new File(filePath);
try {
Scanner freader = new Scanner(inFile);
while (freader.hasNextLine()) {
for (int j = 1; j <= pass.length; j++) {
pass[j][0] = freader.nextLine();
pass[j][1] = freader.nextLine();
pass[j][2] = freader.nextLine();
}
}
freader.close();
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
Login input:
private void input() throws InterruptedException, FileNotFoundException {
for (int i = 0; i < pass.length; i ++){
System.out.println(i);
if (i == pass.length){
add();
}
c.print("Please enter the website: ");
pass[i][0] = c.readLine();
c.print("Please enter your username: ");
pass[i][1] = c.readLine();
c.print("Please enter your password: ");
pass[i][2] = c.readLine();
while (true){
c.clear();
synchronized (c) {
c.println("To continue adding logins, press C. To exit the program press ESC.");
c.println(pass[i][0] + " " + pass[i][1] + " " + pass[i][2]);
}
if (c.isKeyDown(KeyEvent.VK_C)){
break;
}
else if (c.isKeyDown(KeyEvent.VK_ESCAPE)){
fileWriter();
pass();
}
Thread.sleep(10);
}
}
}
Any help is greatly appreciated!! Thanks!
ArrayIndexOutOfBoundsException? In what line ? What ispass? How big is it ? What type is it ?private String pass[][] = new String[1][3];for (int j = 1; j <= pass.length; j++)I think this is wrong, tryfor (int j = 0; j <= pass.length; j++). 0 instead of 1.