wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes extract data from .csv Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "extract data from .csv" Watch "extract data from .csv" New topic
Author

extract data from .csv

Srikanth Ravuri
Greenhorn

Joined: Jan 26, 2011
Posts: 12
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,
Prakash Krsihnan
Greenhorn

Joined: Jan 19, 2006
Posts: 4
Inside while check for linenumber is zero then ignore the operation, else proceed as usuals


"The greatest use of life is to spend it for something that will outlast it"
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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.


Mohamed Sanaulla | My Blog
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38044
    
  22
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

Joined: Jan 26, 2011
Posts: 12
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,
Sreelatha Sankaranarayanan
Greenhorn

Joined: Dec 10, 2010
Posts: 9
This is what will help what you want , note the first 2 lines after the while loop starts -----------

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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

Joined: Jan 26, 2011
Posts: 12
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

Joined: Jan 26, 2011
Posts: 12
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

Joined: Jan 26, 2011
Posts: 12
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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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

Joined: Jan 26, 2011
Posts: 12
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
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
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: 38044
    
  22
If you use readLine once before the loop, that will give you the first line, and you can then proceed to the loop.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
jQuery in Action, 2nd edition
 
subject: extract data from .csv
 
Similar Threads
using javax API to send file contents to COM1
How to read csv file & which collection should be used ?
Text File to ArrayList for JTextArea?
check values in a csv file in all files in a directory
Printing to COM1