Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help reading .csv file into an arraylist

807600Nov 15 2007 — edited Nov 17 2007
i need to read a csv file into an arraylist and then print the arraylist to the screen.

using:
try {
            // Setup our scanner to read the file at the path c:\test.txt
            Scanner myscanner = new Scanner(new File("\\carsDB.csv"));     

ArrayList<CarsClass> carsClass = new ArrayList<CarsClass>(" write all fields");    
            while (myscanner.hasNextLine()) {

myscanner.add(" add car fields to the carClass");              // Write your code here.
            }

// loop to read them all to the screen
csv file is like this:

Manufacturer,carline name,displ,cyl,fuel,(miles),Class
CHEVROLET,CAVALIER (natural gas),2.2,4,CNG,120,SUBCOMPACT CARS
HONDA,CIVIC GX (natural gas),1.6,4,CNG,190,SUBCOMPACT CARS
FORD,CONTOUR (natural gas),2,4,CNG,70,COMPACT
FORD,CROWN VICTORIA (natural gas),4.6,8,CNG,140/210*,LARGE CARS
FORD,F150 PICKUP (natural gas) - 2WD,5.4,8,CNG,130,STANDARD PICKUP TRUCKS 2WD
FORD,F250 PICKUP (natural gas) - 2WD,5.4,8,CNG,160,STANDARD PICKUP TRUCKS 2WD
FORD,F250 PICKUP (natural gas) - 2WD,5.4,8,CNG,150/210*,STANDARD PICKUP TRUCKS 2WD
FORD,F150 PICKUP (natural gas) - 4WD,5.4,8,CNG,130,STANDARD PICKUP TRUCKS 4WD
FORD,F250 PICKUP (natural gas) - 4WD,5.4,8,CNG,160,STANDARD PICKUP TRUCKS 4WD
FORD,E250 ECONOLINE (natural gas) - 2WD,5.4,8,CNG,170,"VANS, CARGO TYPE"
FORD,E250 ECONOLINE (natural gas) - 2WD,5.4,8,CNG,80,"VANS, CARGO TYPE"
FORD,F150 LPG - 2WD,5.4,8,LPG,290/370*,STANDARD PICKUP TRUCKS 2WD
FORD,F250 LPG - 2WD,5.4,8,LPG,260/290/370**,STANDARD PICKUP TRUCKS 2WD

ok, so do i need to write a file carsclass.java or is the line:

ArrayList<CarsClass> carsClass = new ArrayList<CarsClass>(" write all fields");

going to define my carsclass objects? how do i write my fields for each of the 7 categories?

i believe i can easily add to and print the arraylist, but i'm confused how to go about creating this arraylist in the first place. do i just create carclass.java and save them all as strings? i guess my question is mainly how should i structure this? any suggestions/help is appreciated.

Edited by: scottc on Nov 15, 2007 5:55 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 15 2007
Added on Nov 15 2007
14 comments
2,229 views