Mike
Mike

Reputation: 2339

skip inserting first line of csv file

I have a method that takes data from a .csv file and puts it into an array backwards (first row goes in last array slot) however I would like the first row in the .csv file to not be in the array. How would I accomplish this? Here is my code thus far:

public static String[][] parse(String symbol) throws Exception{

    String destination = "C:/"+symbol+"_table.csv"; 

    LineNumberReader lnr = new LineNumberReader(new FileReader(new File(destination)));
    lnr.skip(Long.MAX_VALUE);

    String[][] stock_array = new String[lnr.getLineNumber()][3];

    try{
        BufferedReader br = new BufferedReader(new FileReader(destination));
        String strLine = "";
        StringTokenizer st = null;

        int line = lnr.getLineNumber()-1;

        while((strLine = br.readLine()) != null){           
            st = new StringTokenizer(strLine, ",");

            while(st.hasMoreTokens()){                  
                stock_array[line][0] = st.nextToken();
                        st.nextToken();
                stock_array[line][1] = st.nextToken();
                stock_array[line][2] = st.nextToken();
                        st.nextToken();
                        st.nextToken();
                        st.nextToken();
            }
            line--; 
        }
    }
    catch(Exception e){
        System.out.println("Error while reading csv file: " + e);
    }
    return stock_array;
}

Upvotes: 0

Views: 3521

Answers (2)

bchetty
bchetty

Reputation: 2241

Your code is not efficient, as far as my knowledge goes. Also, you are using linenumberreader.skip(long.max_value), which is not a correct/confirmed way to find the line count of the file. StringTokenizer is kind of deprecated way of splitting tokens. I would code it, in the following way:

public static List<String[]> parse(String symbol) throws Exception {
        String destination = "C:/"+symbol+"_table.csv";
        List<String[]> lines = new ArrayList<String[]>();

        try{
            BufferedReader br = new BufferedReader(new FileReader(destination));
            int index = 0;

            while((line = br.readLine()) != null){
                if(index == 0) {
                   index++;
                   continue; //skip first line
                }

                lines.add(line.split(","));
            }

            if(lines != null && !lines.isEmpty()) {
                Collections.reverse(lines);
            }
        } catch(IOException ioe){
            //IOException Handling
        } catch(Exception e){
            //Exception Handling
        }

        return lines;
    }

Upvotes: 1

lins314159
lins314159

Reputation: 2520

You can skip the first line by just reading it in and doing nothing. Do this just before your while loop:

br.readLine();

To make sure that your array is the right size and lines get stored in the right places, you should also make these changes:

String[][] stock_array = new String[lnr.getLineNumber()-1][3];
...
int line = lnr.getLineNumber()-2;

Upvotes: 2

Related Questions