java - 可能是 line.containes() 在 Android 中缺少搜索词吗?

标签 java android

我正在编写的文本解析器发生了一些奇怪的事情,我想也许你们这些专家可以发现我做错了什么。

解析器正在文本中搜索多个搜索词,并将找到的结果复制到 SD 卡上的输出解析文件中。

代码是:

String line; // line reading buffer
…

Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));

         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

…

while ((line = bufferedReader.readLine()) != null){

            line = bufferedReader.readLine(); // reading the buffer

          /*** parsing the buffer line by line   ***/


                if (line.contains("SearchTermA")){  
                    // parsing the text in this line
                }

                if (line.contains("SearchTermB")){
                    // parsing the text in this line
                }


                // Check that we got to a cretin part in the file
                if (line.contains("SearchTerm_text")){
                     textID = 1;    
                }

                if (textID == 1){
                  if (line.contains("SearchTermC")){
                       // parsing the text in this line
                                                     }
                }

现在,问题是在文件的开头(文件很长)这工作正常,但有时 SearchTermB 会出现在原始文本中,但不会被代码欺骗。我尝试在目标 android 机器上使用 Eclipse 单步执行代码,我可以清楚地看到“行”包含 SerchTermB 但调试器忽略此 IF 语句并继续执行下一个 IF 语句。

可能是 line.containes() 缺少搜索词?

请帮助找出我做错了什么,因为这让我晚上无法休眠......

谢谢,

最佳答案

您的 while 循环一次读取两行...

while ((line = bufferedReader.readLine()) != null){

    line = bufferedReader.readLine(); // reading the buffer

换句话说,您在 while 语句中调用 readLine() 以检查它不是一个 null 结果,然后您立即再次调用 readLine()

摆脱这个...

    line = bufferedReader.readLine(); // reading the buffer

...看看会发生什么。

关于java - 可能是 line.containes() 在 Android 中缺少搜索词吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8831610/

相关文章:

android - MPAndroidChart 设置当前可见的 X 轴

android - 将控制权从 html 传递到 android 以调用 android 方法

java - 远程 OSGI 服务调用

java - 从 EDT 调用 invokeAndWait

android - Socket EADDRINUSE(地址已被使用)

android - "context"到底是什么意思?对于 soundPool.load int load(Context context, int resId, int priority)

android - 无法启动 Activity ComponentInfo Caused by : android. view.InflateException

java - 通用分词器

java - 算法设计: inserting bags into containers with a limited size

java - 将对象添加到列表