0

我有一些问题。在这里,我想在 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();
    }
}

有人请帮忙,非常感谢。

4

2 回答 2

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);
}
于 2013-09-25T07:17:30.403 回答
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);
                }
            }
        }
    }
于 2013-09-25T07:24:04.320 回答