簡體   English   中英

如何計算csv文件中一行的字符數

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

我有一個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

正如您所看到的,我已經手動計算了字符並在其旁邊寫了 HEX 值。 現在我需要用 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();

我建議你閱讀String.split的 javadoc 。 我認為你在這樣做時誤解了這個概念:

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

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

避免在代碼中使用“魔法”數字,例如int[] a = new int[24]; . 為什么是 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));
    }
}

就像我之前說的,這是解決這個問題的一種方法。 您應該嘗試其他選項來解決此問題以提高您的知識...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM