2015-08-17 43 views
0

我想比较编辑文本的值与文件中的所有行(该文件是单词列表)。从文本文件中读取值并与edittext进行比较

我已经做到了这一点,

 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是文本框的值。

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

你是什么意思'它不工作'?不循环?它没有正确比较它们吗? – Ben

+0

我的意思是那些敬酒没有显示。我有2敬酒如果找到这个词和没有匹配的词 –

+0

给与Passiondroid的答案去吧,我认为应该工作 – Ben

回答

0

试试这个

ss.equalsIgnoreCase(line.toLowerCase()) 

替换 “==” 要么 “equalsIgnoreCase” 或 “equals

+0

它没有工作。 –

0

您正在阅读的线的两倍,以及使用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(); 
      } 
     } 
+0

其实问题是文件没有打开。看到我更新的代码 –

+0

如果你得到异常,那么你可以在这里添加堆栈跟踪 – Passiondroid

0

试试这个...

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

在你的代码中,它不断地逐行比较,直到它到达你的最后一行。 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(); 
     } 

希望这将帮助你..

相关问题