如何更改 txt 文件中的数据(字符串)以便我可以对该数据进行数学运算?

How to change data(string) from a txt file so I can do mathematical operations on that data?

对于我的问题,我看到一个提醒,很多人已经问过这个问题,但我似乎找不到合适的答案。或者答案是用其他编程语言写的,这对我没有帮助。所以我再问一遍:)

我必须读取一个 txt 文件,然后对数据进行一些数学运算。此数据为 10000 行,每行由分号分隔的 20 个数字。 为了从 txt 文件中读取,我使用了 BufferedReader,它给了我字符串。我可能必须将此字符串更改为 int。到目前为止,一切都很好。然后我必须把它变成一个数组……怎么做?数组需要是二维的还是一维就够了?

然后最后我不得不把结果写在不同的txt文件里。我必须写入可被 3、11 和 5 整除的不同文件编号,然后我必须对 txt 文件中的每一行求和,并对整个 txt 编号求和。 我到现在为止写的可以吗?我应该删除一些东西还是?

class Assignment {

    public static void main (String[] args) {

        String nameFile = "numbers.txt";
        String contentFile = readFromFile(nameFile);
        List<Integer> divideNumber = stringToIntList(contentFile);

        List<Integer> divisibleNo = processedData(divideNumber);

        System.out.println(divideNumber);
    }

    private static String readFromFile(String nameFile) {
        String content = "";
        try {
            File file = new File(nameFile);
            BufferedReader read = new BufferedReader(new FileReader(file));

            String line = read.readLine();
            while(line != null) {
                System.out.println(line);
                line = read.readLine();
                content = line;
            }
            read.close();
        } catch (IOException e) {
            System.out.println("There was an error reading from the file");
        }
        return content;
    }

    private static List<Integer> stringToIntList(String contentFile) {
        return Arrays
            .stream(contentFile.split("; "))
            .map(Integer::parseInt)
            .collect(Collectors.toList());
    }

    private static int processedData(int divideNumber) {
        try {
            for(int i = 0; i < divideNumber; i++) {
                if((divideNumber % 3 == 0) || (divideNumber % 5 == 0) || (divideNumber % 11 == 0)) {
                System.out.println(divideNumber);
                }
            }
        }
        catch(NumberFormatException e){
            System.out.println("It is not a number"); 
        }
        return divideNumber;
    }
}

使用带有适当分隔符的扫描仪:

List<Integer> list = new ArrayList<>();
Scanner scanner = new Scanner(new FileInputStream(nameFile))
   .useDelimiter("\D+");
while (scanner.hasNext()) {
    int i = scanner.nextInt();
    list.add(i);
}