简体   繁体   English

Java:如果一行包含特定单词,则将其打印一次

[英]Java: Printing a line once if it contains specific words

I want to take in a text file (given by args[0]) and line by line, check for words given by all the arguments after args[0] and print each line with an asterisk if it contains said words. 我想输入一个文本文件(由args [0]给定)并逐行输入,检查args [0]之后所有参数给出的单词,并在每行包含星号的情况下打印一个星号。

I'm pretty sure the code I have at the moment is good enough to check for one word, but I'm unsure how to check for if a line contains multiple words(given by the args) and only to print the line once if it does. 我很确定我目前的代码足以检查一个单词,但是我不确定如何检查一行是否包含多个单词(由args提供),并且仅在出现以下情况时才打印该行是的。

I supposed that a for loop might work, except it would print a line multiple times if it happened to contain multiple words. 我以为for循环可能会起作用,除非它碰巧包含多个单词,否则会多次打印一行。

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

public class WordFinder
{
    public static void main(String args[])
    {
        File file = new File(args[0]);
        Scanner in = null;
        try
        {
            in = new Scanner(file);
            while(in.hasNext())
            {
                String line = in.nextLine();
                if (line.contains(args[1]))
                {
                    System.out.println("* " + line);
                }
                else
                {
                    System.out.println("  " + line);
                }
            }
        }
        catch (FileNotFoundException e)
        {
            System.out.println("Cannot find " + args[0])
            return;
        }
    }
}

Either you want to check for all (and) 您要检查所有(和)

            boolean foundAll = true;
            for (int i = 1; i < args.length && foundAll; ++i) {
                foundAll = foundAll && line.contains(args[i]);
            }
            if (foundAll)
            {
                System.out.println("* " + line);
            }
            else
            {
                System.out.println("  " + line);
            }

Or at least one match (or) 或至少一场比赛(或)

            boolean foundAny = false;
            for (int i = 1; i < args.length; ++i) {
                foundAny = line.contains(args[i]);
                if (foundAny) break;
            }
            if (foundAny)
            {
                System.out.println("* " + line);
            }
            else
            {
                System.out.println("  " + line);
            }

Below is the code which works for all words found. 下面的代码适用于所有找到的单词。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class WordFinder {
    private static final String DEFAULT_LINE_INDICATOR = "  ";
    private static final String ALL_WORDS_FOUND_INDICATOR = "* ";

    public static void main(String args[]) {
        File file = new File(args[0]);
        Scanner in = null;
        try {
            in = new Scanner(file);
            while (in.hasNext()) {
                boolean allWordsFound = true;
                String line = in.nextLine();
                for (int index = 1; index < args.length; index++) {
                    allWordsFound = line.contains(args[index]);
                    if (!allWordsFound) {
                        break;
                    }
                }
                String modifiedLine = allWordsFound ? ALL_WORDS_FOUND_INDICATOR + line : DEFAULT_LINE_INDICATOR + line;
                System.out.println(modifiedLine);
            }
        } catch (FileNotFoundException e) {
            System.out.println("Cannot find " + args[0]);
        }
    }
}

If you want any word, you can use the following code 如果您想输入任何单词,可以使用以下代码

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class WordFinder {
    private static final String DEFAULT_LINE_INDICATOR = "  ";
    private static final String WORD_FOUND_INDICATOR = "* ";

    public static void main(String args[]) {
        File file = new File(args[0]);
        Scanner in = null;
        try {
            in = new Scanner(file);
            while (in.hasNext()) {
                boolean atLeastOneWordFound = false;
                String line = in.nextLine();
                for (int index = 1; index < args.length; index++) {
                    atLeastOneWordFound = line.contains(args[index]);
                    if (atLeastOneWordFound) {
                        break;
                    }
                }
                String modifiedLine = atLeastOneWordFound ? WORD_FOUND_INDICATOR + line : DEFAULT_LINE_INDICATOR + line;
                System.out.println(modifiedLine);
            }
        } catch (FileNotFoundException e) {
            System.out.println("Cannot find " + args[0]);
        }
    }
}

将args放入集合中,并检查文本中的每个单词,直到找到匹配项或eol。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Java不打印行中的所有单词(当单词被添加到ArrayList时)? - Why is Java not printing all the words in the line (when words are added to an ArrayList)? 在Java中打印与字符串等效的特定于字符的行 - Printing a character specific line equivalent to string in Java 使用Java IO搜索并打印特定行 - Searching for and printing a specific line using Java IO 在 Java 中逐行读取和替换特定单词的问题 - Issue with reading and replacing specific words line by line in Java 从输入文本文件中的字符串中分离单词并将其打印以在Java中逐行输出文本文件 - Separating words from a string in input textfile and printing it to output textfile line by line in Java 在java(在终端窗口)中打印时是否可以返回到特定行? - Is it possible to go back to a specific line while printing in java (In Terminal Window)? 如果文件中包含Java中的特定数据,则需要从文件中删除一行文本 - need to delete a line of text from a file if it contains specific data in Java 从 java 中的文本文件打印包含特定模式的行 - print a line that contains a specific pattern from a text-file in java Java行正在打印重复项 - line of Java is printing duplicates 在java的同一行中打印 - printing in the same line in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM