使用line.contains()查找与特定关键字数组匹配的特定行

时间:2013-09-25 07:04:55

标签: java string readline

我有一些问题。在这里,我想在line.contains()中使用string [],之前我尝试了一个代码,如果我放入line.contains(String),它会读取关键字,但我只能一次输入一个关键字。所以,我尝试将关键字分组在一个数组中,但主要问题是line.contains()无法读取String []。

以下是代码: -

package components;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class matchWord {

    public static void main(String[] args) {
        File file = new File("file.txt");
        String[] errorType = {"uuid=A5H50AV_promo1, for domain=null", "Start node"};
        Scanner scanner = null;
        try {
            scanner = new Scanner(file);
        } catch (FileNotFoundException ex) {
            System.out.println("File not found");
        }

        int count = 0;
        //now read the file line by line
        for (int i = 0; i < errorType.length; i++) {
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                //it doesn't read the errorType[i] as i can see that the count=0
                if (line.contains(errorType[i])) {
                    count++;
                    if (count == 1) {
                        System.out.println("Error Description :" + line);
                    }
                }
            }
        }

        System.out.println("Error Type : " + errorType + "\nCount : " + count);
        scanner.close();
    }
}

有人请帮助,非常感谢。

2 个答案:

答案 0 :(得分:1)

交换for和while循环:

   //now read the file line by line
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine(); //also be sure to put this outside the for loop
        for (int i = 0; i < errorType.length; i++) {
            if (line.contains(errorType[i])) {
                count++;
                if (count == 1) { //also this 
                    System.out.println("Error Description :" + line);
                }
            }
        }
    }

问题在于外部循环正在对要检查的错误消息进行交互,并且内部循环正在迭代文件的行。

所以在第一个循环中,程序会检查文件中的所有行对照数组中的第一个元素,一直到文件的末尾。从下一个循环开始,扫描仪已经在文件末尾,因此无法再读取 - 所以内部循环甚至不会被执行一次......

此外,这看起来很臭:

if (line.contains(errorType[i])) {
    count++;
    if (count == 1) { //also this 
        System.out.println("Error Description :" + line);
    }
}

count == 1条件只有一次。仅打印第一条错误消息。如果您需要所有错误消息(因为它看似合乎逻辑),您应该以这种方式处理此部分:

if (line.contains(errorType[i])) {
    count++;
    System.out.println("Error number: " + count + " Error Description :" + line);
}

答案 1 :(得分:0)

将您的代码更改为以下

        int count = 0;         
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();                      
            if (Arrays.asList(errorType).contains(line)) {
                count++;
                if (count == 1) {
                    System.out.println("Error Description :" + line);
                }
            }
        }

而不是

        for (int i = 0; i < errorType.length; i++) {
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            //it doesn't read the errorType[i] as i can see that the count=0
            if (line.contains(errorType[i])) {
                count++;
                if (count == 1) {
                    System.out.println("Error Description :" + line);
                }
            }
        }
    }