【问题标题】:How separate and compare the strings from a txt file如何从 txt 文件中分离和比较字符串
【发布时间】:2014-03-19 17:40:44
【问题描述】:

我需要你的帮助!

我在一个 txt 文件中混合了任何信息:

我必须按照下面的规则来打分

  • 答对2分;
  • -1 分错误答案;
  • 未回答问题的分数(带空格)

并根据以下等级对总分进行分类

  • 40~36 =>> A
  • 35~31 =>> B
  • 30~26 =>> C

我的代码是:

    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();
        }              
    }
}

【问题讨论】:

    标签: android string compare


    【解决方案1】:

    你可以这样做

    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;
        }
    

    【讨论】:

    • 不客气。我已经为你留下了打印部分:) 只需遍历 ArrayLists 并打印每个元素。
    • 不管怎样,帮了我很多,非常感谢我的朋友,大大的拥抱。
    • 您想在哪里打印?
    猜你喜欢
    • 2016-03-11
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 2021-07-06
    相关资源
    最近更新 更多