1

I try a write a java program to import a csv file and put the data to array, and need to convert the string to date and double. last I want to print out the result to the screen to show whether my program work well or not.

It is actually one of my uni. homework which the user need to input the date as args.

After I wrote all code and tried to run it, it showed the following error message:

Exception in thread "main"java.lang.ArrayIndexOutofBoundsException: 0 
    at ReadCSV.main(ReadCSV.java:10)

Can someone tell me what is the mistake that I made in the program??

  import java.util.*;
  import java.text.*;'
  import java.io.*;
  import java.text.ParseException;

 public class ReadCSV {
 public static void main(String[] args) throws IOException, ParseException{
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date startDate, endDate;
    startDate = sdf.parse(args[0]);
    endDate = sdf.parse(args[1]);
    File file = new File("table.csv");
    Scanner inputfile = new Scanner(file);
    inputfile.next();
    inputfile.next();
    int count ; 
    count = NumRows();
    Date[] date_tr = new Date [count];
    Double[] open = new Double[count];
    Double[] high = new Double[count];
    Double[] low = new Double[count];
    Double[] close = new Double[count];
    int i = 0;

    while(inputfile.hasNext()){
        String data_row = inputfile.next();
        String[] data = data_row.split(",");
        date_tr[i]=sdf.parse(data[0]);
        open[i]=Double.parseDouble(data[1]);
        high[i]=Double.parseDouble(data[2]);
        low[i]=Double.parseDouble(data[3]);
        close[i]=Double.parseDouble(data[4]);
        SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println( outFormat.format(date_tr[i]) + " " + open[i] + " " + high[i] + " "+ low[i]+ " " + close[i]);
        i++;
    }
 inputfile.close();
}
  public static int NumRows() throws IOException{
      File file = new File("table.csv");
        Scanner inputfile = new Scanner(file);
        inputfile.next();
        inputfile.next();
        int count = 0; 
        while (inputfile.hasNext()){
            String data_row = inputfile.next();
            count++; 
        }

    return count;
  }

}

2
  • You should tag language. It helps others to find the question and also affects code syntax coloring. Commented Nov 7, 2014 at 18:18
  • Sorry, because I am totally new to java and this website, so I don't know how to type the post that look best to others. Commented Nov 8, 2014 at 8:37

1 Answer 1

2

You will need to pass the command line argument. Since you are not passing command line argument, your code is failing here:

startDate = sdf.parse(args[0]);
endDate = sdf.parse(args[1]);

It's trying to find argument at index 0 and 1 which you didn't passed.

You would need to run command like:

java ReadCSV 2014-09-28 2014-10-28
Sign up to request clarification or add additional context in comments.

5 Comments

yes, that's something that I want to do "java ReadCSV 2014-09-28 2014-10-28" but how can I pass the command line argument ???
So here 2014-09-28 and 2014-10-28 are two command line argument which in your program you read as args[0] and args[1]
then how should i correct the code to make the program read the 2014-09-28 as a date?
it's already reading it in args[0]. So with java ReadCSV 2014-09-28 2014-10-28, in your program args[0] = 2014-09-28 and args[1] = 2014-10-28 so if you want to change the date change it according to position when you supply above command. Please refer docs.oracle.com/javase/tutorial/essential/environment/…
Thank you for your reply. I try to run the program again, by inputting "java ReadCSV 2014-05-01 2014-10-31", the program can run, put when it prints the data out, the date format is weird which show sth like "0019-04-05" ... i think the code to standardize the date format has some errors. I will read the link that you have provided and see how to fix it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.