简体   繁体   English

如何使用BufferedReader.readLine()。split(“”)Java?

[英]How to use BufferedReader.readLine().split(“ ”) java?

I have this code: 我有以下代码:

public static void opticFiberApplication() throws IOException {
    int v1;
    File file = new File("src/Arcos.txt");
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    int capacity = Integer.parseInt(br.readLine());
    Graph application = new Graph(capacity);

    while ((v1 = Character.getNumericValue(br.read())) != -1) {
        int v2 = Character.getNumericValue(br.read());
        int cost = Character.getNumericValue(br.read());
        if (application.getVertex(v1) == null) application.addVertex(v1);
        if (application.getVertex(v2) == null) application.addVertex(v2);

        application.addEdge(v1, v2, cost);
    }
    printGraph(application);
    prim(application);
}

And the .txt Arcos 和.txt Arcos

5
0110025032143238

Where the first line is the capacity and the second one are the vertex and the cost of the edge. 第一条线是容量,第二条线是顶点和边的成本。


I want to know how to use BufferedReader.readLine().split(" ") so that Arcos.txt can be read like this 我想知道如何使用BufferedReader.readLine()。split(“”),以便可以像这样读取Arcos.txt

5
0 1 10 0 2 5 0 3 2 1 4 3 2 3 8

Thanks for your help. 谢谢你的帮助。

I think you are confused as to what Split is. 我认为您对Split是什么感到困惑。

public String[] split(String regex) - Splits this string around matches of the given regular expression. public String[] split(String regex) -在给定正则表达式的匹配项周围拆分此字符串。 What you are looking for is this: 您正在寻找的是:

for (int i = 0;i < str.length(); i++){
    System.out.println(str.charAt(i));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM