简体   繁体   中英

Read CSV file and use StringTokenizer

This is a simple homework assignment that has been driving me crazy for the last few days. If I were to use a couple of Arrays I could have done it a while ago but having to use the StringTokenizer is driving me nuts.

The main problem I am having is reading the CSV file. I don't know how to do it and previous searches online have only brought forward super intense solutions that are too much for a beginner like me to handle.

This is my code. As you can see, I don't know whether use .nextLine() or .NextToken() . Neither seems to work.

For those wondering the assignment is basically reading the first 4 products separated by commas and reading the rest of the rows as the rating of those 4 products. Basically 6 rows with 4 columns. The first row is the products the rest are the ratings.

import java.util.StringTokenizer;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ProductRating {

public static void main(String[] args) {
    // TODO Auto-generated method stub


    Scanner fileIn=null;
    try{
        fileIn = new Scanner(
                 new FileInputStream("C:/Users/Cristian/Desktop"));
    }
    catch (FileNotFoundException e)
     {  // This block executed if the file is not found
        // and then the program exits
    System.out.println("File not found.");
    System.exit(0);
    }

    //If Opened File Successful this runs
    String products = "A";
    String rantings ="0";
    System.out.println("Gathering Ratings for Products");

    do{

        String delimiters = ", ";

        StringTokenizer gatherProducts[]=new StringTokenizer[inputLine, delimeters];
        gatherProducts=fileIn.nextLine();

    }while(fileIn.hasNextLine()==true);

}   

}

easy way with Streams API in java 8 (csv with a header line):

Path path = Paths.get("C:/Users/Cristian/Desktop"); // path to folder
    Path file = path.resolve("file.csv"); // filename 
    Stream<String> lines = Files.lines(file);
    List<String[]> list = lines
            .skip(1)
            .map(line -> line.split(","))
            .collect(Collectors.toList());

You can also use the flatMap function to retrieve all elements in a single list

         List<String> list = lines
            .skip(1)
            .map(line -> line.split(","))
            .flatMap(Arrays::stream)
            .collect(Collectors.toList());

First of all, I don't think you're interpreting the StringTokenizer method calls / return types correctly. The array doesn't make much sense to me.

You want to iterate over each line in the csv file, right? You can just create a new StringTokenizer on each step of the loop, and use that to grab what you want from each line.

Clean up your do ... while loop so it looks more like this:

final String delimiter = ", ";
for(int lineNumber = 1; fileIn.hasNextLine(); ++lineNumber) {
    String csvLine = fileIn.next();
    if (lineNumber == 1) {
      // this is your special case for the first line, handle it!
      continue;
    }

    StringTokenizer tokenizer = new StringTokenizer(csvLine, delimiter);
    while (tokenizer.hasMoreTokens()) {
      String token = tokenizer.nextToken();
      // do stuff with the tokens!
    }
}

Why you are using array of StringTokenizer? Try this.

try{
            StringTokenizer st=null;
            FileReader inputFileReader = new FileReader("C:/Users/Cristian/Desktop");
            BufferedReader inputStream = new BufferedReader(inputFileReader);
            String inLine = null;
            while((inLine =  inputStream.readLine())!=null){
                st = new StringTokenizer(inLine, ",");
                System.out.println(st.nextToken());
                System.out.println(st.nextToken());
                System.out.println(st.nextToken());
            }
        }
        catch (FileNotFoundException e)
         {  // This block executed if the file is not found
            // and then the program exits
        System.out.println("File not found.");
        System.exit(0);
        }

Use SuperCSV , example follows, from this page :

private static void readWithCsvBeanReader() throws Exception {

        ICsvBeanReader beanReader = null;
        try {
                beanReader = new CsvBeanReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE);

                // the header elements are used to map the values to the bean (names must match)
                final String[] header = beanReader.getHeader(true);
                final CellProcessor[] processors = getProcessors();

                CustomerBean customer;
                while( (customer = beanReader.read(CustomerBean.class, header, processors)) != null ) {
                        System.out.println(String.format("lineNo=%s, rowNo=%s, customer=%s", beanReader.getLineNumber(),
                                beanReader.getRowNumber(), customer));
                }

        }
        finally {
                if( beanReader != null ) {
                        beanReader.close();
                }
        }
}

You will need to define a bean for your business object, and use it in lieu of CustomerBean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM