Member Avatar for mehnihma

I have a problem with writting this program

I need to read a csv file and write it to binary then read it again. I has to be written in this way:

Field name Data type
Name First Text
Name Last Text
Birth Day Whole number
Birth Month Whole number
Birth Year 4-digit year
Weight Whole number
Height Number with decimal part

I now how to read csv and binary, but how to do this is a mistery to me.

What toubles me the most is that in this csv first line is just strings and then coninues name lastname and so on

namefirst namelast birthday birthmonth birthyear weight height

Starlin Castro 243199019072.8
Madison Bumgarner 18198921576.0
Jason Heyward 98198924077.3
Ruben Tejada 2710198916071.2
Jenrry Mejia 1110198916072.5
Mike Stanton 811198923577.9
Dayan Viciedo 103198924071.1
Chris Sale 303198917077.2
Freddie Freeman 129198922577.7
Clayton Kershaw 193198822575.4
Travis Snider 22198823572.0
Elvis Andrus 268198820072.0
Trevor Cahill 13198822076.0
Rick Porcello 2712198820077.0
Brett Anderson 12198823576.5
Fernando Martinez 1010198820073.0
Jhoulys Chacin 71198821575.2
Chris Tillman 154198820077.8
Neftali Feliz 25198821575.5
Craig Kimbrel 285198820571.6

Can you help me?

this is my code till now

I can read it line by line but how to continue now?

Code blocks are created by indenting at least 4 spaces
... and can span multiple lines

import java.io.*;
import java.util.*;

public class read {

  public static void main(String[] args) {

     try
     {

                    //csv file containing data
        String strFile = "BaseballNames1.csv";

                    //create BufferedReader to read csv file
        BufferedReader br = new BufferedReader( new FileReader(strFile));
        String strLine = "";
        StringTokenizer st = null;
        int lineNumber = 0;
        int tokenNumber = 0;

                    //read comma separated file line by line
        while( (strLine = br.readLine()) != null)
        {
           lineNumber++;

                            //break comma separated line using ","
           st = new StringTokenizer(strLine, ",");

           while(st.hasMoreTokens())
           {
                                    //display csv values
              tokenNumber++;
                          /*                                                      

              String name = dis.readUTF();
              String lastName = dis.readUTF();
              int bDay = dis.readInt();
              int bMonth = dis.readInt();
              int bYear = dis.readInt();
              int weight = dis.readInt();
              double height = dis.readDouble();         */          



                 System.out.println("tokel line:"+st.nextToken());  




           }

                            //reset token number
           tokenNumber = 0;

        }


     }
        catch(Exception e)
        {
           System.out.println("Exception while reading csv file: " + e);                  
        }
  }

}

Recommended Answers

All 3 Replies

You might have to edit the CSV file.

Or, if you know all the lengths are the same, you can parse the second value and seperate by the values from string index 1-3, 3-6, ect...

What do you mean by:

Quoted Text Here
write it to binary
Can you give an example?

Member Avatar for mehnihma

I have succeeded to do it, thanks anyway

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.