我试图从一个文本文件中读取,但这些字符串被分成了不同的属性,但我不知道在第一次拆分后该如何操作。
下面是我的代码:getType()字符串的偏移量应该是多少?
try {
InputStream is = context.getAssets().open("Autoeval");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
//Skips lines
for (int i = 0; i< questionNumber; i++) {
reader.readLine();
}
question = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getId() {
return question.substring(0, question.indexOf(";"));
}
public String getType() {
return question.substring(question.indexOf(";"));
}发布于 2013-01-30 08:18:15
这是丑陋的,但为什么不创建两个全局私有变量:
private String _id;
private String _type;然后,在阅读完问题后,您可以这样做:
{
//stuff
question = reader.readLine();
_id = question.substring(0, question.indexOf(";"));
_type = question.substring(_id.length); // assuming no other ";" delimiters
}
public String getId() {
return _id;
}
public String getType() {
return _type;
}尽管如此,还有更好的方法来做到这一点。
https://stackoverflow.com/questions/14519027
复制相似问题