2015-06-11 56 views
-4

我有一個Justice_League.csv文件,其中包含四行逗號。我想要統計每行中的字符數,並將該數字轉換爲十六進制數。如何統計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 

正如你可以看到我已經handcounted人物和旁邊寫下十六進制值給它。現在我需要在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(); 
+3

你能描述你到目前爲止?它工作嗎?你有哪些部位有問題?基本上,如果你有一個真正的問題,這將是最好的。不僅僅是一個「爲我而做」的問題類型。這樣你會得到更好的答案。 – sstan

+1

什麼不工作? – mhlz

+0

說FileNotFoundException。請幫我解決這個問題。所以我可以看看其餘的代碼是否工作。 – user3123222

回答

1

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

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

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

避免在代碼中使用'神奇'的數字,如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)); 
    } 
} 

就像我之前說過的,這是解決這個問題的方法。你應該嘗試另一種方法來解決這個問題,以提高你的知識...

+0

謝謝你。我甚至沒有想過使用.replaceAll方法。 – user3123222

相關問題