0

I am trying to read a CSV file and have it display the contents as a basic list for an Android app. I am using the method given by Kopfgeldjaeger in this thread.

I have added a couple of 'toasts' which either display 'success' or 'fail' at the bottom of the Android screen if the code managed (or didn't manage) to load the CSV file properly. See below:

  try {
      CSVReader reader = new CSVReader(new InputStreamReader(getAssets().open("file.csv")));
      for(;;) {
          next = reader.readNext();
          if(next != null) {
              list.add(next);
          } else {
              break;
          }
      }
      Toast.makeText(getApplicationContext(), "SUCCESS",
              Toast.LENGTH_SHORT).show();
  } catch (IOException e) {
      e.printStackTrace();
      Toast.makeText(getApplicationContext(), "FAIL",
              Toast.LENGTH_SHORT).show();
  }

When I load the app, I get the 'SUCCESS' message, so all is well so far. Now, I'd like to see if I can load any of the data. In Kopfgeldjaeger's answer, it is suggested that I could access a string using the following code:

list.get(1)[1]

So, in order to check that it's worked, I try to generate another toast, as follows:

      Toast.makeText(getApplicationContext(), list.get(1)[1],
              Toast.LENGTH_SHORT).show();

This added toast causes the program to fail to load properly. The question is, have I gotten the toast syntax wrong, or is my CSV file not loading properly?

Community
  • 1
  • 1
CaptainProg
  • 5,610
  • 23
  • 71
  • 116
  • Have you tried to change the `list.get(1)[1]` to `list.get(0)[0]`? – Wroclai Aug 26 '12 at 19:06
  • 1
    Try going in debug mode and debug the code line by line. That way you'll know if the list is still null or actually populated. – Avinash Aug 26 '12 at 19:07
  • 2
    In the for loop try sys.out() for each list item. Also, check after the loop is completed, how many list items are there (list.size()). Also, try putting some logcat message here to find if it is throwing ArrayIndexOutOfBounds exception or anything else? – rahul Aug 26 '12 at 19:08
  • By jove, @Shelly, you're right. I feel a fool. – CaptainProg Aug 26 '12 at 19:10

1 Answers1

0

There are a couple of things to check:

  1. Make sure your csv file has a size of at least 2 x 2 entries, otherwise retrieving the data from line index 1 and column index 1 won't work. For example, print or debug list.size() and list.get(0).length to see if they're both at least 2.
  2. Confirm that your csv file is actually comma separated, and not e.g. semicolon separated. I have seen occassions where certain software seems to choose its own delimiter.

As a recommendation: the referenced csv reader is part of ByteCode's OpenCSV. You may want to include the latest source code or jar from that project. It supports custom delimiter characters and also provides a shorthand for parsing all the csv data into a list of string arrays:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
List myEntries = reader.readAll();
MH.
  • 45,303
  • 10
  • 103
  • 116
  • Thanks very much, it was a case of my CSV file being tab delimited as opposed to comma separated. So, it was loading a 1 dimensional vector and trying to read reference [1,1] (or [2,2] in human terms!). I've changed the format of my CSV file to the correct standard now. – CaptainProg Aug 26 '12 at 19:34