java.lang.NumberFormatException:对于输入字符串:"" |为道路/城市做Dijkstra算法

时间:2016-06-03 08:58:08

标签: java algorithm graph dijkstra numberformatexception

我试图读取2个文件并将它们存储在邻接矩阵中。但是,我收到的错误是:

线程中的异常" main" java.lang.NumberFormatException:对于输入字符串:""

at java.lang.NumberFormatException.forInputString(Unknown Source)

at java.lang.Integer.parseInt(Unknown Source)

at java.lang.Integer.parseInt(Unknown Source)

在project3.Project3.storeData(Project3.java:36)

36 | int number = Integer.parseInt(tokens[0]);

在project3.Project3.main(Project3.java:64) 这是(在我的主要方法中)

64 | storeData();

我的storeData方法:

public static void storeData() throws IOException {
    digraph = new Digraph();

    File file1 = new File("city.dat");
    Scanner cities = new Scanner(file1);
    File file2 = new File("road.dat");
    Scanner roads = new Scanner(file2);
    while(cities.hasNext()) {
        String delims = "[ ]+";
        String[] tokens = cities.nextLine().split(delims);
        if(tokens.length > 5){
            int number = Integer.parseInt(tokens[0]);
            String code = tokens[1];
            String cityName = tokens[2] + " " + tokens[3];
            int pop = Integer.parseInt(tokens[4]);
            int elev = Integer.parseInt(tokens[5]);
            digraph.add(new City(number, code, cityName, pop, elev));
        }
        else {
            int number = Integer.parseInt(tokens[0]);
            String code = tokens[1];
            String cityName = tokens[2];
            int pop = Integer.parseInt(tokens[3]);
            int elev = Integer.parseInt(tokens[4]);
            digraph.add(new City(number, code, cityName, pop, elev));
        }
    }
    while(roads.hasNext()) {
        digraph.insertRoad(roads.nextInt() -1, roads.nextInt() -1, roads.nextInt());
    }
    cities.close();
    roads.close();
}

我想弄清楚为什么会这样,所以我可以继续我的其余程序。任何帮助将非常感谢!谢谢!

1 个答案:

答案 0 :(得分:0)

不要使用nextLine(),请尝试使用next()和nextInt()。