2017-04-05 115 views
0

我正在使用文件读取器读取csv文件,csv文件的第二列是一个rgb值,例如rgb(255,255,255),但csv文件中的列是用逗号分开。如果我使用逗号deliminator,它会读像“RGB(255,”所以我怎么看完整RGB值,代码粘贴下面。谢谢!读取存储在csv文件中的rgb值,用逗号分隔符分隔

 FileReader reader = new FileReader(todoTaskFile); 
     BufferedReader in = new BufferedReader(reader); 

     int columnIndex = 1; 
     String line; 

     while ((line = in.readLine()) != null) { 
      if (line.trim().length() != 0) { 
       String[] dataFields = line.split(","); 
       //System.out.println(dataFields[0]+dataFields[1]); 
       if (!taskCount.containsKey(dataFields[columnIndex])) { 
        taskCount.put(dataFields[columnIndex], 1); 
       } else { 
        int oldCount = taskCount.get(dataFields[columnIndex]); 
        taskCount.put(dataFields[columnIndex],oldCount + 1); 
       } 
      } 

回答

0
line = "rgb(25,255,255)"; 
    line = line.replace(")", ""); 
    line = line.replace("rgb(", ""); 
    String[] vals = line.split(","); 

投瓦尔斯的值整数,然后你可以用它们

0

这里是你如何能做到这一点:

Pattern RGB_PATTERN = Pattern.compile("rgb\\((\\d{1,3}),(\\d{1,3}),(\\d{1,3})\\)"); 

String line = "rgb(25,255,255)"; 
Matcher m = RGB_PATTERN.matcher(line); 
if (m.find()) { 
    System.out.println(m.group(1)); 
    System.out.println(m.group(2)); 
    System.out.println(m.group(3)); 
} 

这里

\\d{1,3} => match 1 to 3 length digit 
(\\d{1,3}) => match 1 to 3 length digit and stored the match 

虽然()是元字符,我们必须逃避它。

+0

非常感谢!它通过使用你的方法起作用。但是,如何通过使用rgb值来设置饼图的样式来调用group()来绘制饼图?谢谢!我正在使用一个控制器,并在initialize()中调用read方法和plot方法,唯一错误的是饼图的颜色。 –

+0

如果你想获得红色使用'm.group(1)',绿色使用'm.group(2)',蓝色使用'm.group(3)' –

+0

是的,我试着打电话给m 。组();在initialize()方法中设置饼图的样式,但它不会识别m.group()。 –

1

我强烈建议不要使用自定义方法来解析CSV输入。有专门的图书馆为你做。

@Ashraful Islam发布了一个很好的方式来解析来自“单元格”(我重复使用它)的值,但获取这个“单元格”原始值必须以不同的方式完成。该草图显示了如何使用apache.commons.csv库进行操作。

package csvparsing; 

import org.apache.commons.csv.CSVFormat; 
import org.apache.commons.csv.CSVRecord; 

import java.io.FileReader; 
import java.io.IOException; 
import java.io.Reader; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class GetRGBFromCSV { 

    public static void main(String[] args) throws IOException { 
     Reader in = new FileReader(GetRGBFromCSV.class.getClassLoader().getResource("sample.csv").getFile()); 
     Iterable<CSVRecord> records = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(in); // remove ".withFirstRecordAsHeader()" 
     for (CSVRecord record : records) { 
      String color = record.get("Color"); // use ".get(1)" to get value from second column if there's no header in csv file 
      System.out.println(color); 

      Pattern RGB_PATTERN = Pattern.compile("rgb\\((\\d{1,3}),(\\d{1,3}),(\\d{1,3})\\)", Pattern.CASE_INSENSITIVE); 

      Matcher m = RGB_PATTERN.matcher(color); 
      if (m.find()) { 
       Integer red = Integer.parseInt(m.group(1)); 
       Integer green = Integer.parseInt(m.group(2)); 
       Integer blue = Integer.parseInt(m.group(3)); 
       System.out.println(red + " " + green + " " + blue); 
      } 
     } 

    } 

} 

这是一个自定义的有效CSV输入这将可能使基于正则表达式的解决方案表现出乎意料:

Name,Color 
"something","rgb(100,200,10)" 
"something else","rgb(10,20,30)" 
"not the value rgb(1,2,3) you are interested in","rgb(10,20,30)" 

有很多,你可能会忘记,当你编写自定义考虑到期权解析器:带引号和不带引号的字符串,引号内的分隔符,引号内的引号转义,不同的分隔符(,;),多列等等。第三方csv解析器会为您处理这些事情。你不应该重新发明轮子。

+0

非常感谢!它有很大帮助! –

+0

[Ok; - )](https://stackoverflow.com/help/someone-answers) –