简体   繁体   English

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

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

I have a Justice_League.csv file that has four lines with commas between them.我有一个Justice_League.csv 文件,其中有四行,中间用逗号。 I want to count the number of characters there are in each line and convert that number to hex.我想计算每行中的字符数并将该数字转换为十六进制。

Below is the contents of 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

As you can see I have handcounted the characters and wrote the HEX value next to it.正如您所看到的,我已经手动计算了字符并在其旁边写了 HEX 值。 Now I need this done in Java.现在我需要用 Java 完成。 This is what I have so far.这是我到目前为止。 Can anybody help me out?有人可以帮我吗?

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();

I suggest you to read the javadoc of String.split .我建议你阅读String.split的 javadoc 。 I think that you misunderstood the concept when you did this:我认为你在这样做时误解了这个概念:

String[] f=line.split(","); String[] f=line.split(",");

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

Avoid using 'magic' numbers in your code like int[] a = new int[24];避免在代码中使用“魔法”数字,例如int[] a = new int[24]; . . Why 24?为什么是 24?

Well, here comes a version that do what you want to do.好吧,这里有一个可以做你想做的事情的版本。 Maybe it isn't the best way to do this but it works.也许这不是最好的方法,但它有效。

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

Like I said previously, this is a way to solve this.就像我之前说的,这是解决这个问题的一种方法。 You should try another options to solve this in order to improve your knowledge...您应该尝试其他选项来解决此问题以提高您的知识...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM