数字格式异常,真的卡住了

时间:2013-11-23 22:11:39

标签: java exception

这里是我的代码....我得到一个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];

1 个答案:

答案 0 :(得分:4)

使用接受Scanner的{​​{1}}构造函数,以便扫描程序实例不使用File来源:

String

由于您已经有此参考,您可以使用

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