我有一个简单的问题
我有一个java文本文件,其中包含以下记录:
Hamada 115599
Johny 1478523
Bosy 753621
此文本文件定义了java程序中许多帐户的用户名和密码。
我写了一个简单的代码来编辑用户帐户的密码。
System.out.println("Change Account Password");
System.out.printf("Username: ");
String user4 = input.next();
System.out.printf("Password: ");
int pass4 = input.nextInt();
boolean checkaccount = false;
Scanner x = null;
try {
x = new Scanner(new File("C:\\Users\\فاطمة\\Downloads\\accounts.txt"));
while (x.hasNext()) {
String a = x.next();
int b = x.nextInt();
if ((a == null ? user4 == null : a.equals(user4)) && pass4 == b) checkaccount = true;
}
if (checkaccount) {
int newpass = 0;
boolean checked = true;
File f = new File("C:\\Users\\فاطمة\\Downloads\\accounts.txt");
File tempFile2 = new File("C:\\Users\\فاطمة\\Downloads\\accounts2.txt");
BufferedWriter writer2 = new BufferedWriter(new FileWriter(tempFile2));
Scanner sc = new Scanner(f);
Scanner sc2 = new Scanner(System. in );
int foo = Integer.parseInt(user4);
while (sc.hasNext()) {
String currentLine1 = sc.nextLine();
String[] tokens = currentLine1.split(" ");
if (Integer.valueOf(tokens[0]) == foo && checked) {
sc2.nextLine();
System.out.printf("New Password: ");
newpass = sc2.nextInt();
currentLine1 = tokens[0] + " " + newpass;
checked = false;
}
writer2.write(currentLine1 + System.getProperty("line.separator"));
}
writer2.close();
sc.close();
f.delete();
boolean successfull1 = tempFile2.renameTo(f);
if (successfull1 == true) System.out.println("Password changed successfully.");
else System.out.println("Error occurred during changing password.");
} else System.out.println("Wrong username or password... try again !!");
} catch (Exception e) {}
首先,程序检查帐户是否存在,如果是,则程序允许用户更改密码,但是当我运行此代码时没有任何反应,并且没有显示输出语句"新密码: "
这段代码有什么问题?
答案 0 :(得分:1)
如果你在此处获得例外 - 请不要在代码中吞下它。用最后一行替换最后一行:
catch (Exception e) {
e.printStackTrace()
}
您可以更好地了解正在发生的事情。