對於我的計算機科學課,我正在製作一個網站用戶名/密碼程序。我決定使用一個2D字符串數組,並沒有找到最好的解決方案。我試圖讓文件讀取器讀取已寫入的登錄名,但我一直在收到ArrayIndexOutOfBoundsException錯誤。我的文件閱讀器代碼如下,並且還包括我的登錄輸入代碼。我剛剛啓動Java,因此我擁有非常基本的編程知識。二維陣列文件閱讀器
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);
}
登錄輸入:
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);
}
}
}
任何幫助是極大的讚賞!謝謝!
你從哪裏初始化2D陣列? –
「ArrayIndexOutOfBoundsException」在哪裏?在哪一行?什麼是「通行證」?它有多大 ?它是什麼類型? – UDKOX
@UDKOX它從1行開始,但每次用戶添加新登錄時增加1。 – cfgfr0st