java - 为什么这个不会显示?

标签 java arrays file-io

它不会显示包含该单词的代码行。 我希望它显示包含子字符串“put”的代码行,不区分大小写。应打印第一行和第二行,因为它们都包含子字符串“put”。 这是文本文件:

Test input.

Put text here.

Not here.

Run it on a computer.

import java.io.*;
import java.util.*;

public class Put {
    public static void main(String[] args) {
        String firstTextFile = "PROG06.in.txt";
        String secondTextFile = "PRG06.out.txt";
        Scanner Document = null;
        PrintWriter NewFile = null;
        try {
            Document = new Scanner(new File(firstTextFile));
            NewFile = new PrintWriter(new FileOutputStream(secondTextFile, true));
        } catch (Exception e) {
            System.out.println("Could not find " + firstTextFile);
            System.exit(0);
            System.out.println("Could not find " + secondTextFile);
            System.exit(0);
        }
        while (Document.hasNextLine()) {
            String[] words = Document.nextLine().split(" ");
            List<String> wordList = Arrays.asList(words);
            if (wordList.contains("put")) {
                NewFile.print(wordList);
            }
        }
        Document.close();
        NewFile.close();
    }
}

最佳答案

if (wordList.contains("put")) { 只会匹配单词“put”,而不匹配“Put”。您要么需要对所有内容执行多个 if/else 语句,要么需要在拆分原始字符串之前对其执行 .toLowercase

关于java - 为什么这个不会显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16697128/

相关文章:

java - JPA 与 Hibernate 4.x,@ElementCollection,主键

java - 从代码中触发选择事件

java - 无法使用 wagon maven FTPS 传输中型大文件

c - 如何让我的数组返回一个 int 数组以在另一个函数中使用?

rust - 返回 Result<Cow<[u8]>> 而不是 Result<Vec<u8>>

java - 无法使用 API 29 android studio 在模拟器中运行应用程序

javascript - 数组过滤器和数组显示

C 编程 - 整数/长整型到字符串表示

java - 用于数据库实现的内存映射MappedByteBuffer或直接ByteBuffer?

batch-file - 批量将 100 个文件夹中的所有文件向上移动一个文件夹