Note that there are some explanatory texts on larger screens.

plurals
  1. POPulling values from an CSV file in java
    text
    copied!<p>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:</p> <pre><code>Acton Town (District),Acton Town (Piccadilly),2.00 Aldgate (Circle),Aldgate (Metropolitan),4.00 Aldgate East (District),Aldgate East (Hammersmith And City),2.00 </code></pre> <p>I am trying to import it into my java class using the following method:</p> <pre><code>public ArrayList&lt;Connection&gt; 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; } </code></pre> <p>If I run this, the app will crash and I can't figure out why. If I set <code>lineData.setText(endingStation)</code> as <code>lineData.setText(connection)</code>, it will display the entire row e.g:</p> <pre><code>Acton Town (District),Acton Town (Piccadilly),2.00 </code></pre> <p>If I comment out the </p> <blockquote> <p>duration = Float.parseFloat(tokens.nextToken());</p> </blockquote> <p>it will run the method. Can anyone find any fault with this method?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload