如何计算csv文件中一行中的字符数 [英] How to count the number of characters in a line in a csv file

查看:426
本文介绍了如何计算csv文件中一行中的字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



下面是Justice_League.csv的内容:

  Bruce Wayne,蝙蝠侠,无,Gotham City,Robin,小丑43 2B 
Oliver皇后,绿箭,无,星城, ,Deathstroke 50 32
Clark Kent,超人,飞行,大都市,无,Lex Luthor 46 2E
Bart Allen,闪电,速度,中央城市,儿童Flash,教授缩放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();


解决方案

建议您阅读 String.split 。我认为你误解了这个概念:


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

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


避免在代码中使用'magic'数字,例如 int [] a = new int [24]; 。为什么24?



好吧,这里有一个版本,做你想做的。也许这不是最好的方法,但它的工作原理。

  public void convertCSVToFlat()throws Exception {
String csv =Justice_League.csv;
BufferedReader bReader = new BufferedReader(new FileReader(csv));

//我们将值存储在这个数组列表中,
//但更好的方法是使用对象来保存'b
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);
//我假设你不想计算逗号和空格。
//如果你想,注释下一行
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();

//只是为了显示结果
for(int i = 0; i< lines.size(); i ++){
System.out.print get(i));
System.out.print(\t+ chars.get(i));
System.out.println(\t+ hex.get(i));
}
}

像我前面说的,这是一种解决方法这个。你应该尝试另一个选项来解决这个问题,以提高你的知识...


I have a Justice_League.csv file that has four lines with commas between them. 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:

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. Now I need this done in 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. I think that you misunderstood the concept when you did this:

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

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

Avoid using 'magic' numbers in your code like int[] a = new int[24];. Why 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...

这篇关于如何计算csv文件中一行中的字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆