【问题标题】:Importing and Splitting CSV input, Need helping reading only first character of导入和拆分 CSV 输入,需要帮助仅读取第一个字符
【发布时间】:2017-04-05 23:31:44
【问题描述】:

在做一个类项目,数据读入数组后出现问题。

在 Excel 电子表格中,我需要直接阅读一列,只注意每个块中的第一个数字。当我拆分列时,它是一个没有空格的连续字符串,所以我不知道如何只计算第一个数字而不是计算每个数字(就像现在的代码一样)。 示例:(11, 20, 296) 被拆分,变为“1120296”

        final int[] benfordLaw = {0, 30, 18, 13, 10, 8, 7, 6, 5, 5};
        int[] actualPrecent = new int[10];
        int benfordScore = 0;
        float[] countForNum = new float[10];
        float totalCount = 0;

        //This while loop will read through each line of the file
        while (scan.hasNextLine()) {

            // This statement reads in the next line of the file.
            String line = scan.nextLine();

            String[] values = line.split(",(?=(?:(?:[^\"]*+\"){2})*+[^\"]*+$)");


            //12-15 is the column #'s
            for (int i = 12; i < 15; ++i) {
                for (int j = 0; j < values[i].length(); j++) {
                    char charA = values[i].charAt(j);
                    for (int k = 1; k <= 9; ++k){
                        char compare = (char)(k + '0');
                        if (charA == compare) {
                            ++countForNum[k];
                            ++totalCount;
                        }
                    }
                }
            }
        }

【问题讨论】:

    标签: java arrays excel csv


    【解决方案1】:

    我想我并没有完全明白你的问题,但希望对你有所帮助

    例子:(11, 20, 296) 被拆分,变成"1120296" => 如果只想得到文本中的11

    读取 Line 并获取字符串 溶胶1。) a) 删除第一个和最后一个字符 b) Split.Text 并修剪它。 c) 拿到第一个

    溶胶 2。) 使用 RegEx 并获取第一个元素 ex RegEx.) ([0-9]*),

    祝你好运

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多