0

I am having some trouble pulling values from a CSV file for android app that I am working on. The CSV file takes the following format:

Acton Town (District),Acton Town (Piccadilly),2.00
Aldgate (Circle),Aldgate (Metropolitan),4.00
Aldgate East (District),Aldgate East (Hammersmith And City),2.00

I am trying to import it into my java class using the following method:

public ArrayList<Connection> importConnections(){
        try 
        {
            //gets the lines.txt file from assets
            in = this.getAssets().open("connections.csv");
            Scanner scan = new Scanner(in);
            TextView lineData = (TextView)findViewById(R.id.displayLine);
            String connection = null;
            String startingStation = null;
            String endingStation = null;
            Float duration = 0;
            do
            {
                connection = scan.nextLine();
                String delimiter = ",\n";
                StringTokenizer tokens = new StringTokenizer(connection, delimiter);
                startingStation = tokens.nextToken();
                endingStation = tokens.nextToken();
                duration = Float.parseFloat(tokens.nextToken());
                connections.add(startStation, endStation, duration);
            }
            while(scan.hasNext());
            //for testing purposes
            lineData.setText(endingStation);
        } 
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return connections;
    }

If I run this, the app will crash and I can't figure out why. If I set lineData.setText(endingStation) as lineData.setText(connection), it will display the entire row e.g:

Acton Town (District),Acton Town (Piccadilly),2.00

If I comment out the

duration = Float.parseFloat(tokens.nextToken());

it will run the method. Can anyone find any fault with this method?

6
  • 2
    Several things if you want help. Can you post the logcat? Can you tell us what line is failing? Did you run it in the debugger and see what is causing it to fail? If I were to make a guess, it is the delimiter you used. I think it should be just ",". The Readline should consume the \n I think. Commented Mar 1, 2013 at 14:15
  • Do you not have an exception name and stack trace? That's what would tell you why the error happens. Commented Mar 1, 2013 at 14:28
  • @Kaediil which part of the logcat do you want? I have never posted a logcat up here before. Commented Mar 1, 2013 at 14:57
  • @entonio the stack trace is returning null Commented Mar 1, 2013 at 15:02
  • This line connections.add(start, end, duration); doesn't compile. Where are start and end defined? Should this be startingStation endingStation? Commented Mar 1, 2013 at 15:11

1 Answer 1

3

You could try using OpenCSV http://opencsv.sourceforge.net/

It is pretty simple to use and returns an array of each row.

CSVReader reader = new CSVReader(new FileReader(<filepath>));

String[] temp;
int NUM = #; //number of rows in csv, or insert code to read through file until null.

for(int i = 0; i < NUM; i++)
{               
    temp = reader.readNext();               //read next line into temp
}

System.out.println(temp[0]); //Acton Town (District)
System.out.println(temp[1]); //Acton Town (Piccadilly)
System.out.println(temp[2]); //2.00 (string)


reader.close();

Like I said, it is easy to use and prevents you from having to parse out the string on your own.

Sign up to request clarification or add additional context in comments.

3 Comments

does OpenCSV support float's? In the example you have given the duration is 2, but the duration could be up to 2 decimal places e.g 3.78
@BenNewton It returns each value as a String. You can use Float.parseFloat(string) to convert it.
Edited the post to show correct output, Geobits' comment answers your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.