如何计算 csv 文件中一行的字符数

How to count the number of characters in a line in a csv file

提问人:user3123222 提问时间:6/11/2015 更新时间:6/12/2015 访问量:1130

问:

我有一个Justice_League.csv文件,它有四行,它们之间有逗号。我想计算每行中的字符数并将该数字转换为十六进制。

以下是Justice_League.csv的内容:

Bruce Wayne,Batman,None,Gotham City,Robin,The Joker                 43      2B
Oliver Queen,Green Arrow,None,Star City,Speedy,Deathstroke          50      32
Clark Kent,Superman,Flight,Metropolis,None,Lex Luthor               46      2E
Bart Allen,The Flash,Speed,Central City,Kid Flash,Professor Zoom    52      34

正如你所看到的,我已经手工计算了字符数,并在它旁边写了十六进制值。现在我需要在 Java 中完成此操作。这就是我目前所拥有的。谁能帮我?

public String convertCSVToFlat (Exchange exchange) throws Exception {
    String csv="Justice_League.csv";
    BufferedReader bReader = new BufferedReader(new FileReader(csv));
    String line = "";
    int count = 0;
    String str[] = new String[200];
    int[] a = new int[24];
    String[] hexNumber = new String[4];
    try {
        bReader.readLine();
        int characterSum = 0;
        int i = 0;  
        while((line = bReader.readLine()) != null) {
             String[] f=line.split(",");
             a[count]=Integer.parseInt(f[2]);
             str[count]=f[1];            
             count++;
             characterSum += line.length();
             hexNumber[i] = Integer.toHexString(characterSum);
             i++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    bReader.close();
    return hexNumber.toString();
Java CSV 十六进制 转换器

评论

3赞 sstan 6/11/2015
你能描述一下你到目前为止所拥有的吗?它有效吗?您在使用哪些部件时遇到问题?基本上,如果您有一个实际问题,那将是最好的。不仅仅是一个“请为我做”类型的问题。这样你会得到更好的答案。
1赞 mhlz 6/11/2015
究竟什么不起作用?
0赞 user3123222 6/11/2015
FileNotFoundException 说。请帮我解决这个问题。所以我可以看看其余的代码是否有效。
0赞 JuanZe 6/11/2015
FileNotFoundException 表示程序找不到该文件。不要使用 String csv=“Justice_League.csv”; 使用 String csv=full_ path_to_“Justice_League.csv”_file; (对每个“/”使用“File.separator)。
0赞 user3123222 6/11/2015
娟泽,我发现你的建议很有帮助。但是现在我得到了一个numberformatexception。线程“main”中的异常 java.lang.NumberFormatException: 对于输入字符串:“None” at java.lang.NumberFormatException.forInputString(未知来源) at java.lang.Integer.parseInt(未知来源) at java.lang.Integer.parseInt(未知来源) at com.raghav.conversion.Conversion.main(Conversion.java:25)

答:

1赞 Roque Possamai 6/12/2015 #1

我建议你阅读 String.split 的 javadoc。我认为您在这样做时误解了这个概念:

字符串[] f=line.split(“,”);

a[count]=Integer.parseInt(f[2]);--> java.lang.NumberFormatException 这里!

避免在代码中使用“魔术”数字,例如 .为什么是24?int[] a = new int[24];

好吧,这里有一个版本可以做你想做的事情。也许这不是最好的方法,但它有效。

public void convertCSVToFlat () throws Exception {
    String csv="Justice_League.csv";
    BufferedReader bReader = new BufferedReader(new FileReader(csv));
    
    //We're storing the values at this 3 arraylists, 
    //but a better approach is using an object to hold'em
    ArrayList<String> lines = new ArrayList<String>();
    ArrayList<Integer> chars = new ArrayList<Integer>();
    ArrayList<String> hex = new ArrayList<String>();
    
    String line = "";
    try {
        while((line = bReader.readLine()) != null) {
            lines.add(line);
            //I'm assuming that you don't want to count the commas and spaces.
            //If you want to, comment the next line
            line = line.replaceAll(",", "").replaceAll(" ", "");
            int c = line.length(); //count remaining chars...
            chars.add(c);
            hex.add(Integer.toHexString(c));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    bReader.close();
    
    //Just to show the results
    for (int i = 0; i < lines.size(); i++) {
        System.out.print(lines.get(i));
        System.out.print("\t" + chars.get(i));
        System.out.println("\t" + hex.get(i));
    }
}

就像我之前说的,这是解决这个问题的一种方法。您应该尝试其他方法来解决这个问题,以提高您的知识......

评论

0赞 user3123222 6/12/2015
谢谢你。我什至没有想到使用 .replaceAll 方法。