简体   繁体   English

从文本文件读取到并行Java数组

[英]Reading from text file into parallel Java arrays

I've found a lot of similar questions on Stack Overflow so far, but none of them have actually answered my question. 到目前为止,我已经在Stack Overflow上找到了很多类似的问题,但是没有一个问题能真正回答我的问题。 How do I read the data from different columns in a text file into three separate arrays? 如何将文本文件中不同列的数据读入三个单独的数组中?

Currently my BufferedReader is going through the entire text file and reading it line by line. 目前,我的BufferedReader正在浏览整个文本文件,并逐行读取它。 The problem is, each line is stored as a singular array element. 问题是,每行都存储为单个数组元素。

As you can see, I tried to access the array elements by calling certain data from the "lines[]" array, which contains all of the lines in the file as separate elements. 如您所见,我试图通过调用“ lines []”数组中的某些数据来访问数组元素,该数组包含文件中所有行作为单独的元素。 I just have no idea how to access those elements individually and then store them all in separate arrays. 我只是不知道如何单独访问这些元素,然后将它们全部存储在单独的数组中。 Below are the contents of the text file I'm reading from, and then the Java code itself. 下面是我正在读取的文本文件的内容,然后是Java代码本身。

(Name,Team,Score)
John,Blue,20
Jamie,White,28
Jonathan,Blue,19
Ron,White,39
Ron,Blue,29

-- -

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Bowling7 {

    public static void main(String[] args) {
        try {
            BufferedReader fin = new BufferedReader(new FileReader(new File("bowling.txt")));
            System.out.println("File open successful!");

            int line = 0;
            String[] lines;
            String[] names;
            String[] teams;
            int[] scores;

            for (String x = fin.readLine(); x != null; x = fin.readLine()) {
                lines = x.split(",");
                teams[line] = lines[line];
                scores[line] = lines[line];
                line++;
            }

            System.out.println(lines[0]);
            System.out.println(lines[1]);

        catch (IOException e) {
            System.out.println("File I/O error!");
        }
    }
}

Java arrays have a fixed length, your approach is off because you assume they are dynamic. Java数组的长度是固定的,您的方法是关闭的,因为您认为它们是动态的。 Use a Collection , like List , and you can use Files.readAllLines(Path) to get all of the lines with one call. 使用Collection (如List ,然后可以使用Files.readAllLines(Path)通过一次调用获取所有行。 Remember to skip the first line (the header), and parse the score. 切记跳过第一行(标题)并解析分数。 Like, 喜欢,

File f = new File("bowling.txt");
try {
    List<String> lines = Files.readAllLines(f.toPath());
    List<String> names = new ArrayList<>();
    List<String> teams = new ArrayList<>();
    List<Integer> scores = new ArrayList<>();
    for (String line : lines.subList(1, lines.size())) {
        String[] tokens = line.split("\\s*,\\s*");
        names.add(tokens[0]);
        teams.add(tokens[1]);
        scores.add(Integer.parseInt(tokens[2]));
    }
    System.out.println("Names: " + names);
    System.out.println("Teams: " + teams);
    System.out.println("Scores: " + scores);
} catch (IOException e) {
    e.printStackTrace();
}

I get 我懂了

Names: [John, Jamie, Jonathan, Ron, Ron]
Teams: [Blue, White, Blue, White, Blue]
Scores: [20, 28, 19, 39, 29]

Something like this would work 这样的事情会起作用

 List<String> lines= Files.readAllLines(file.toPath());

    String[] teams=new String[lines.size()],names=new String[lines.size()];
    int[] scores=new int[lines.size()];
    for (int i = 0; i < lines.size(); i++) {
        String[] sections=lines.get(i).split(",");
        names[i]=sections[0];
        teams[i]=sections[1];
        scores[i]= Integer.parseInt(sections[2]);
    }
    System.out.println(Arrays.toString(names));
    System.out.println(Arrays.toString(teams));
    System.out.println(Arrays.toString(scores));

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

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