This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
I want to extract data from .csv file eliminating the 1st line. I tried to put code any number of fashion but couldn't make it happen. Any suggestion on where I been wrong.
Here is my code.
As well I want to insert the data extracted from .csv file into my SQL table. Any suggestion on it. I taught of inputing by reading each element as an array or by reading it as a readLine method.
Don't reinvent the wheel, and use an existing CSV library. You can find some in our AccessingFileFormats FAQ, under Excel. I myself have used opencsv successfully quite a few times.
You need some debugging information. Get rid of the tokenising for the time being (comment it out), and print each line as you read it.
Why are you reusing the file name to read into? Why haven't you got a separate local for the String read?
Srikanth Ravuri
Greenhorn
Joined: Jan 26, 2011
Posts: 12
posted
0
Prakash Krsihnan wrote:Inside while check for linenumber is zero then ignore the operation, else proceed as usuals
I didn't quit get your point. can you please brief it more.
I don't want the 1st line i.e. ID FNAME LNAME DEPTID. Thats where I am stuck.
In that case- You just have to do-
But suggestion is to use the libraries suggested by Rob. I also mentioned a small glitch in the code in my first post. Also Campbell suggested not using StringTokenizer- You can achieve it by using the split() method of the String class.
Srikanth Ravuri
Greenhorn
Joined: Jan 26, 2011
Posts: 12
posted
0
Mohamed Sanaulla wrote:
Srikanth Ravuri wrote:
I don't want the 1st line i.e. ID FNAME LNAME DEPTID. Thats where I am stuck.
In that case- You just have to do-
But suggestion is to use the libraries suggested by Rob. I also mentioned a small glitch in the code in my first post. Also Campbell suggested not using StringTokenizer- You can achieve it by using the split() method of the String class.
Thank you Mohamed
I will keep your inputs in mind and proceed for the next time.
Srikanth Ravuri
Greenhorn
Joined: Jan 26, 2011
Posts: 12
posted
0
Campbell Ritchie wrote:Why are you still using StringTokenizer?
You need some debugging information. Get rid of the tokenising for the time being (comment it out), and print each line as you read it.
Why are you reusing the file name to read into? Why haven't you got a separate local for the String read?
Thank you campbell for your imput. I am new to Java and trying to get into phase. I will look more into string methods available and move forward.
Srikanth Ravuri
Greenhorn
Joined: Jan 26, 2011
Posts: 12
posted
0
Rob Spoor wrote:Don't reinvent the wheel, and use an existing CSV library. You can find some in our AccessingFileFormats FAQ, under Excel. I myself have used opencsv successfully quite a few times.
Thanks Rob
I am new to java and don't have much knowledge in CSV library. I will study more into it and try to excel myself. Thank you once again.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
4
posted
0
If you use readLine once before the loop, that will give you the first line, and you can then proceed to the loop.
1. When you are iterating the file, you do it from the very first line. Say Line 0 (if you had started the counter variable with the value 0). As generally you will have the very first line as header and you are very sure of it, skip the iteration for that line conditionally. Proceed with all the other lines.
2. You seemed to have split the tokens with the fileName variable which is NOT right and it may not give the expected output (Refer Campbell's post).
3. As the CSV is the well known format and there are many other people who had done a research on it, there are tools available to make your job easy. So don't reinvent the wheel. Instead use an existing library.