开发者

Reading Long data from CSV

I have a CSV file, I'm reading it the elements from the file

each line is converted to List, one of the columns is a long data, but Java reads it in

2.01005E+14 format, and I cannot convert it to long, Is there a way to convert only that value to long?

The is the value in the CSV 201005300149580.

The code, my CSV class

 package com.gta.moneyb.util;

import java.util.ArrayList;
import java.util.List;

public class CSV {

   public static final char DEFAULT_SEP = ',';

   /** Construct a CSV parser, with the default separator (`,'). */
   public CSV() {
     this(DEFAULT_SEP);
   }

   /** Construct a CSV parser with a given separator. 
    * @param sep The single char for the separator (not a list of
    * separator characters)
    */
   public CSV(char sep) {
     fieldSep = sep;
   }

   /** The fields in the current String */
   protected List<String> list = new ArrayList<String>();

   /** the separator char for this parser */
   protected char fieldSep;

   /** parse: break the input String into fiel开发者_开发问答ds
    * @return java.util.Iterator containing each field 
    * from the original as a String, in order.
    */
   public List<String> parse(String line)
   {
     StringBuffer sb = new StringBuffer();
     list.clear();      // recycle to initial state
     int i = 0;

     if (line.length() == 0) {
       list.add(line);
       return list;
     }

     do {
             sb.setLength(0);
             if (i < line.length() && line.charAt(i) == '"')
                 i = advQuoted(line, sb, ++i);  // skip quote
             else
                 i = advPlain(line, sb, i);
             list.add(sb.toString());
       i++;
     } while (i < line.length());

     return list;
   }

   /** advQuoted: quoted field; return index of next separator */
   protected int advQuoted(String s, StringBuffer sb, int i)
   {
     int j;
     int len= s.length();
         for (j=i; j<len; j++) {
             if (s.charAt(j) == '"' && j+1 < len) {
                 if (s.charAt(j+1) == '"') {
                     j++; // skip escape char
                 } else if (s.charAt(j+1) == fieldSep) { //next delimeter
                     j++; // skip end quotes
                     break;
                 }
             } else if (s.charAt(j) == '"' && j+1 == len) { // end quotes at end of line
                 break; //done
       }
       sb.append(s.charAt(j));  // regular character.
     }
     return j;
   }

   /** advPlain: unquoted field; return index of next separator */
   protected int advPlain(String s, StringBuffer sb, int i)
   {
     int j;

     j = s.indexOf(fieldSep, i); // look for separator
         if (j == -1) {                 // none found
             sb.append(s.substring(i));
             return s.length();
         } else {
             sb.append(s.substring(i, j));
             return j;
         }
     }
 }


To parse a String containing a long you need to use Long.parseLong()

String text = "201005300149580";
long number = Long.parseLong(text);


The problem is with the CSV file, I created a CSV file in Notepad with the above values and they come up fine


I would always recommend using CSVReader to for the tricky task of parsing and creating csv files.


The long should be correct in memory. You are seeing it wrong when you print it.
Use DecimalFormat with your preferred format to convert it to String and you will see it right

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜