• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

extract data from .csv

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

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.

Thank you,
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inside while check for linenumber is zero then ignore the operation, else proceed as usuals
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the output/error of the code you have posted- ItDoesntWorkIsUseless.

Also note the given code may not work in cases where the data is like
"Firstname, lastname", "phone","location"

If you are looking for CSV parsers- there are lot of libraries for the same. Apache Commons has one CSV parser library.

 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 80645
473
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Srikanth Ravuri
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

Thank you,
 
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what will help what you want , note the first 2 lines after the while loop starts -----------

 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Srikanth Ravuri wrote:

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.

Thank you,



Only If you could tell what's happening with the code.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prakash Krsihnan wrote:Inside while check for linenumber is zero then ignore the operation, else proceed as usuals



In the code- linenumber is used only at two places-To declare and to increment. It doesn't really affect the CSV parsing process.
 
Srikanth Ravuri
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sreelatha Sankaranarayanan wrote:This is what will help what you want , note the first 2 lines after the while loop starts -----------



I am missing in between lines

Output

101 XXXXXX XXXXX 1
103 XXXXXX XXXXX 2
105 XXXXXX XXXXX 3


missing 102 and 104.....
 
Srikanth Ravuri
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohamed Sanaulla wrote:

Prakash Krsihnan wrote:Inside while check for linenumber is zero then ignore the operation, else proceed as usuals



In the code- linenumber is used only at two places-To declare and to increment. It doesn't really affect the CSV parsing process.




This is my output when I execute my program

ID FNAME LNAME DEPTID
106 XXXX XXXX 1
107 XXXX XXXX 2
108 XXXX XXXX 2
109 XXXX XXXX 3
110 XXXX XXXX 3


I don't want the 1st line i.e. ID FNAME LNAME DEPTID. Thats where I am stuck.
 
Srikanth Ravuri
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Srikanth Ravuri wrote:

Sreelatha Sankaranarayanan wrote:This is what will help what you want , note the first 2 lines after the while loop starts -----------



I am missing in between lines

Output

101 XXXXXX XXXXX 1
103 XXXXXX XXXXX 2
105 XXXXXX XXXXX 3


missing 102 and 104.....





I am sorry. It is perfect.

Thank you,
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Srikanth Ravuri
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 80645
473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use readLine once before the loop, that will give you the first line, and you can then proceed to the loop.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good that you had resolved it.

What they had been trying to say was,

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic