Vivek
Vivek

Reputation: 1461

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 fields
    * @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;
         }
     }
 }

Upvotes: 0

Views: 2247

Answers (5)

rajesh
rajesh

Reputation: 1

    double doubleValue = Double.valueOf("2.01005E+14");
long longValue = (long) doubleValue;
String actualValue = String.valueOf(longValue);

Upvotes: 0

Guillermo Vasconcelos
Guillermo Vasconcelos

Reputation: 1701

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

Upvotes: 0

Joel
Joel

Reputation: 30156

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

Upvotes: 0

Vivek
Vivek

Reputation: 1461

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

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533670

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

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

Upvotes: 1

Related Questions