2017-04-05 21 views
0

一类项目工作的第一个字符,它读入一个数组后不会与数据的麻烦。导入和拆分CSV输入,帮助有需要的只读取的

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; 
         } 
        } 
       } 
      } 
     } 

回答

0

我觉得我没有完全得到的是你的问题,但希望它有助于

例子:(11, 20,296)被分割,成为“1120296” =>如果你只是想在文本

读行的11,并获得字符串 索尔1) 一)删除第一个和最后一个字符 b )Split.Text并修剪它。 C)获得第一个

索尔2) 使用正则表达式,并获得第一个元素 前正则表达式。)([0-9] *),

好运