7267881在我的文件中作为帐户,如果您执行
,则扫描文件 System.out.println(Account[2][1]);
它打印回7267881,但是当提示用户是否输入7267881时,它返回它是一个无效的数字...文件中的所有其他帐号都不起作用...请帮我找出原因< / p>
import java.io.File; import java.util.Scanner;
public class AcccountArray {
public static void main(String[] args)
{
//Scan the file and save account details to array
File file = new File ("customers.txt");
System.out.println("Path : " + file.getAbsolutePath());
try{
Scanner scanner = new Scanner(new File("customers.txt"));
String[][] Account = new String[Integer.valueOf(scanner.nextLine())][3];
for(int i=0;i<Account.length;i++)
{
Account[i][0]=scanner.nextLine();
Account[i][1]=scanner.nextLine();
Account[i][2]=scanner.nextLine();
}
scanner.close();
System.out.println(Account[2][1]);
Scanner userinput = new Scanner(System.in);
System.out.println("Please enter account number: ");
String accountNumber = userinput.next();
int matchindex = 0;
Boolean match = false;
for (int k =0;k<Account.length;k++)
{
if(Account[k][1].equals(accountNumber))
{
match = true;
matchindex = k;
}
}
if(match)
{
Account ac = new Account();
ac.toString(Account[matchindex][0], Account[matchindex][1], Account[matchindex][2]);
System.out.println("Enter 'D' for deposite\nEnter 'W' for withdrawal\nEnter 'Q' for quit");
Scanner transaction = new Scanner(System.in);
String type = transaction.next();
Scanner ammount = new Scanner(System.in);
switch (type) {
case "D":
System.out.println("Enter the ammount : ");
float diposit = ammount.nextFloat();
float curent = Float.valueOf(Account[matchindex][2]);
System.out.println("New balance = "+(curent+diposit));
break;
case "W":
System.out.println("Enter the ammount : ");
float withdrawal = ammount.nextFloat();
float balance = Float.valueOf(Account[matchindex][2]);
System.out.println("New balance = "+(balance-withdrawal));
break;
case "Q":
System.out.println("Exit");
break;
default:
System.out.println("Invalid transaction");
}
}
else
{
System.out.println("Invalid user account number");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
文件看起来像
4
John Anderson
4565413
250.00
Louise Carter
2323472
1250.45
Paul Johnson
7267881
942.81
Sarah Wilson
0982377
311.26
答案 0 :(得分:2)
即时错误可以在输入文件中找到。有问题的帐号后面似乎有一个空格。您需要在文件完全中设置帐号,因为它将在探测期间输入,或者在输入时修剪字符串。
但是,您需要学习如何有效地进行调试。我有一些关于如何做到这一点的建议,在Debug Strategy上用Java编写了一个有用的例子。