2013-11-23 30 views
0

hereis我的代碼....我得到一個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) 

li。 15是

String[][] Account = new String[Integer.valueOf(scanner.nextLine())][3]; 
+0

解決了掃描儀的問題,但現在我得到一個FileNotFOiundException ...準備拉頭髮 – user2954611

+0

你不是廁所國王在文件的正確位置(或者更好的資源)。 1+到@ Reimeus的回答。 –

回答

4

使用接受一個File所以Scanner構造掃描儀實例未使用String來源:

Scanner scanner = new Scanner(new File("customers.txt")); 

既然你已經有這樣的參考,您可以使用

Scanner scanner = new Scanner(file); 
+0

現在我得到FileNotFoundException:customers.txt \t at AcccountArray.main(AcccountArray.java:14) – user2954611

+0

但客戶肯定是文件 – user2954611

+1

該文件不在應用程序路徑中。確保文件位於'new File(「customers.txt」)。getAbsolutePath()' – Reimeus