作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要你的帮助!
我在一个 txt 文件中混合了任何信息:
我必须根据以下规则分配分数
将总分按照以下等级分类
我的代码是:
private void readRaw() {
tv.append("\n\nDiretório do arquivo processado: res/raw/history_data.txt:\n");
InputStream is = this.getResources()
.openRawResource(R.raw.history_data);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr, 8192);
try {
String test;
while (true) {
test = br.readLine();
// How do I compare the contents of the first line with the contents of the last 20 characters of each line?
if (test == null)
break;
else
tv.append("\n" + " " + test);
}
isr.close();
is.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
最佳答案
你可以这样做
private void readRaw() {
tv.append("\n\nDiretório do arquivo processado: res/raw/history_data.txt:\n");
InputStream is = this.getResources()
.openRawResource(R.raw.history_data);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr, 8192);
try {
String test, correctAnswer = null;
int lineNo = 0;
ArrayList<String> studentNames = new ArrayList<String>();
ArrayList<String> answers = new ArrayList<String>();
while (true) {
test = br.readLine();
if (test == null)
break;
else
tv.append("\n" + " " + test);
if(lineNo == 0)
{
correctAnswer = test;
}
else
{
String[] contents = test.split(" ", 2);
studentNames.add(contents[0]);
answers.add(contents[1]);
}
lineNo++;
}
for (String string : answers) {
int result = compare(string, correctAnswer);
// get grade according to points and print the information
}
isr.close();
is.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
使用compare()
函数将学生的答案与实际答案进行比较
private int compare(String string, String correctAnswer) {
int totalPoints = 0;
if(string.length() != correctAnswer.length())
{
return totalPoints;
}
for (int i = 0; i < string.length(); i++) {
if(string.charAt(i) == correctAnswer.charAt(i) && string.charAt(i) != ' ')
{
totalPoints += 2;
}
else if(string.charAt(i) != ' ')
{
totalPoints -= 1;
}
}
return totalPoints;
}
关于android - 如何从 txt 文件中分离和比较字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22513912/
我是一名优秀的程序员,十分优秀!