我在从我正在开发的 android 应用程序的 CSV 文件中提取值时遇到了一些问题。CSV 文件采用以下格式:
Acton Town (District),Acton Town (Piccadilly),2.00
Aldgate (Circle),Aldgate (Metropolitan),4.00
Aldgate East (District),Aldgate East (Hammersmith And City),2.00
我正在尝试使用以下方法将其导入我的 java 类:
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;
}
如果我运行它,应用程序将崩溃,我不知道为什么。如果我设置lineData.setText(endingStation)
为lineData.setText(connection)
,它将显示整行,例如:
Acton Town (District),Acton Town (Piccadilly),2.00
如果我注释掉
持续时间 = Float.parseFloat(tokens.nextToken());
它将运行该方法。任何人都可以找到这种方法的任何错误吗?