Java - 将文本文件读取为整数并将每行拆分为Array

时间:2018-04-26 19:55:48

标签: java arrays split

我有这样的文本文件:

1  2
1  4
1  5
2  3
2  6
3  6
4  5
4  7
5  7
7  8
7  9
8  10
9  10

所以我想拆分每个整数并放入Array

        Path objPath = Paths.get("graph.txt");
        if (Files.exists(objPath)){

            File objFile = objPath.toFile();
            try(BufferedReader in = new BufferedReader(
                    new FileReader(objFile))){
                String line = in.readLine();

                while(line != null){


                    String[] stringArray = line.split("\\s+");
                    int[] intArray = new int[stringArray.length];
                    for (int i = 0; i < stringArray.length; i++) {
                        String numberAsString = stringArray[i];
                        intArray[i] = Integer.parseInt(numberAsString);
                    }
                    BridgeInGraph graph_obj = new BridgeInGraph(11);
                    graph_obj.add_edges_to_vertex(intArray[0], intArray[1]);
                    graph_obj.add_edges_to_vertex(intArray[2], intArray[3]);
                    .
                    .
                    .

                    System.out.println("Bridges in  Graphs ");
                    graph_obj.getCutVertices();
                    System.out.println("Points:");
                    graph_obj.AP();

                    line = in.readLine();
                }

            }
            catch(IOException e){

                System.out.println(e);
            }

        }
        else
        {
            System.out.println(
                    objPath.toAbsolutePath() + " doesn't exist");
        }

我写了那段代码,但有些不对劲。

intArray [0]都是第一个整数 intArray [1]都是第二个整数

但我问过intArray [0] = 1 intArray [1] = 2 intArray [2] = 1 intArray [3] = 4 ... intArray [25] = 10

我该怎么办?一些帮助:)

编辑:我将使用intArray作为参数

Edit2:我修复了一些不理解的代码

Edit3:感谢有用的编码@xagaffar。另一件事是,如果11位于文件的顶部,我们怎样才能代替BridgeInGraph graph_obj = new BridgeInGraph(11);

11
1  2
1  4
1  5
2  3
2  6
3  6
4  5
4  7
5  7
7  8
7  9
8  10
9  10


BridgeInGraph graph_obj = new BridgeInGraph(11);
//BridgeInGraph graph_obj = new BridgeInGraph(stringArray[x]);

        Path objPath = Paths.get("graph.txt");
        if (Files.exists(objPath)){

            File objFile = objPath.toFile();
            try(BufferedReader in = new BufferedReader(
                    new FileReader(objFile))){
                String line = in.readLine();

                while(line != null){

                    String[] linesFile = new String[] {line};

                    String[] stringArray = line.split("\\s+");


                    graph_obj.add_edges_to_vertex(
                            Integer.parseInt(stringArray[0]),
                            Integer.parseInt(stringArray[1]));

                    line = in.readLine();
                }
                System.out.println("Bridges in  Graphs ");
                graph_obj.getCutVertices();
                System.out.println("Points:");
                graph_obj.AP();
            }
            catch(IOException e){

                System.out.println(e);
            }
        }
        else
        {
            System.out.println(
                    objPath.toAbsolutePath() + " doesn't exist");
        }

3 个答案:

答案 0 :(得分:0)

您可以使用Scanner,其中包含阅读nextInt()的方法:

public static void main(Strin... arg) {
    int[] numbers = readNumbers(new FileInputStream(new File("graph.txt")));
}

public static int[] readNumbers(InputStream in) {
    Scanner scan = new Scanner(in);
    List<Integer> numbers = new ArrayList<>();

    while (scan.hasNext())
        numbers.add(scan.nextInt());

    return toIntArray(numbers);
}

private static int[] toIntArray(List<Integer> numbers) {
    int i = 0;
    int[] arr = new int[numbers.size()];

    for (int num : numbers)
        arr[i++] = num;

    return arr;
}

答案 1 :(得分:0)

    Path objPath = Paths.get("graph.txt");
    if (Files.exists(objPath)){
        File objFile = objPath.toFile();
        try(BufferedReader in = new BufferedReader(
                new FileReader(objFile))){
            String line = in.readLine();
            List<Integer> numbers = new ArrayList<>();

            while(line != null){
                String[] stringArray = line.split("\\s+");
                for (int i = 0; i < stringArray.length; i++) {
                    String numberAsString = stringArray[i];
                    numbers.add(Integer.parseInt(numberAsString));
                }

                line = in.readLine();
            }

        }
        catch(IOException e){
            System.out.println(e);
        }

    }
    else
    {
        System.out.println(
                objPath.toAbsolutePath() + " doesn't exist");
    }

答案 2 :(得分:0)

第一个问题是你在循环中声明你的obj_graph,你基本上在每个循环中创建一个新对象。在循环之外声明它。

第二个问题是你正在访问不在其中的数组元素(intArray [2],intArray [3])。

这可以解决您的问题。

    BridgeInGraph graph_obj = new BridgeInGraph(11);

    Path objPath = Paths.get("graph.txt");
    if (Files.exists(objPath)) {

        File objFile = objPath.toFile();
        try (BufferedReader in = new BufferedReader(
                new FileReader(objFile))) {
            String line = in.readLine();

            while (line != null) {

                String[] stringArray = line.split("\\s+");
                graph_obj.add_edges_to_vertex(
                       Integer.parseInt(stringArray[0]),
                       Integer.parseInt(stringArray[1]));

                line = in.readLine();
            }

        } catch (IOException e) {

            System.out.println(e);
        }

    } else {
        System.out.println(
                objPath.toAbsolutePath() + " doesn't exist");
    }