【问题标题】:Reading a value from textfile and comparing with edittext从文本文件中读取一个值并与edittext进行比较
【发布时间】:2015-08-17 10:39:04
【问题描述】:

我想将编辑文本的值与文件中的所有行进行比较。(文件是单词列表)。

我已经做到了,

        try{
            final InputStream file = getAssets().open("words.txt");
            reader = new BufferedReader(new InputStreamReader(file));
            String line = reader.readLine();
            while(line != null){

                line = reader.readLine();
                if(ss == line.toLowerCase()){
                    Toast.makeText(this, "Working!", Toast.LENGTH_SHORT)
                            .show();
                }
                else{
                    Toast.makeText(this, "Not found !", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        } catch(IOException ioe){
            ioe.printStackTrace();
        }

这里 ss 是 textfield 的值。

 String ss = (res.getText()).toString();

这是 words.txt 文件 https://raw.githubusercontent.com/eneko/data-repository/master/data/words.txt

但是上面的代码不起作用。

编辑:

我检查了文件是否打开,问题是文件没有打开。

        try{
            final InputStream file = getAssets().open("words.txt");
            Toast.makeText(this, "File Opened", Toast.LENGTH_SHORT)
                    .show();
            reader = new BufferedReader(new InputStreamReader(file));
            String line ;
            while((line = reader.readLine()) != null){

                if(ss.equalsIgnoreCase(line)){
                    Toast.makeText(this, "Working!", Toast.LENGTH_SHORT)
                            .show();
                    TextView tv = myTextViewList.get(counter);
                    tv.setText(line);
                }
                else{
                    Toast.makeText(this, "Not found !", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        } catch(IOException ioe){
            ioe.printStackTrace();
        }

【问题讨论】:

  • “它不工作”是什么意思?是不是循环播放?是不是比较正确?
  • 我的意思是那些祝酒词没有显示。如果找到这个词并且没有匹配的词,我有 2 个祝酒词
  • 试一试 Passiondroid 的回答,我认为应该可行
  • 使用 .equals() 代替 ==

标签: android file-handling word-list


【解决方案1】:

试试这个

ss.equalsIgnoreCase(line.toLowerCase())

将“==”替换为“equalsIgnoreCase”或“equals

【讨论】:

    【解决方案2】:

    您正在阅读该行两次以及使用 equalsIgnoreCase 比较字符串

            while((line = reader.readLine()) != null){
    
                if(ss.equalsIgnoreCase(line)){
                    Toast.makeText(this, "Working!", Toast.LENGTH_SHORT)
                            .show();
                }
                else{
                    Toast.makeText(this, "Not found !", Toast.LENGTH_SHORT)
                            .show();
                }
            }
    

    【讨论】:

    • 实际上问题是文件没有被打开。查看我更新的代码
    • 如果您遇到异常,那么您可以在此处添加堆栈跟踪
    【解决方案3】:

    试试这个...

    BufferedReader in = new BufferedReader(new FileReader(filepath));
    boolean found = false;
    while (( line = in.readLine()) != null)
    {
    if (line.contains(str))
    {
        found = true;
        break; //break out of loop now
    }
     }
     in.close();
    
    if (found)
    {
        System.out.println("Yes");
    }
    else
    {
      System.out.println("No");
    BufferedWriter out = new BufferedWriter(new FileWriter(filepath,true));
    out.newLine();
    out.write(str);
    out.close();
    }
    

    【讨论】:

      【解决方案4】:

      在您的代码中,它不断地逐行比较,直到它到达您的最后一行(即 235886),并且它将在循环中连续运行。 它一一检查所有行,因此当您第一次比较时,如果成功则中断循环, 并且还使用 .equals 进行字符串比较而不是 '=='

      String ss = "a";
              try{
                  final InputStream file = getAssets().open("words.txt");
                  BufferedReader reader = new BufferedReader(new InputStreamReader(file));
                  String line = reader.readLine();
                  while(line != null){
      
                      line = reader.readLine();
                      if(ss.equals(line)){
                          Toast.makeText(this, "Working!", Toast.LENGTH_SHORT)
                                  .show();
                          break;
                      }
                      else{
                          Toast.makeText(this, "Not found !", Toast.LENGTH_SHORT)
                                  .show();
                          break;
                      }
                  }
              } catch(IOException ioe){
                  ioe.printStackTrace();
              } 
      

      希望对你有所帮助..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-10
        • 2012-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多