这是我的代码。...我正在获取NumberFormatException和idk whow解决它..新手,完全卡住了,真的需要帮助
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("customers.txt");
String[][] Account = new String[Integer.valueOf(scanner.nextLine())][3];
for(int i=0;i<Account.length;i++)
{
Account[i][0]=scanner.nextLine();
//System.out.println(Account[i][0]);
Account[i][1]=scanner.nextLine();
//System.out.println(Account[i][1]);
Account[i][2]=scanner.nextLine();
//System.out.println(Account[i][2]);
}
scanner.close();
错误:
java.lang.NumberFormatException: For input string: "customers.txt"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.valueOf(Unknown Source)
at AcccountArray.main(AcccountArray.java:15)
。 15是
String[][] Account = new String[Integer.valueOf(scanner.nextLine())][3];
最佳答案
对接受Scanner
的File
使用构造函数,以使扫描程序实例不使用String
源:
Scanner scanner = new Scanner(new File("customers.txt"));
由于您已经有了此参考,因此可以使用
Scanner scanner = new Scanner(file);