[英]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.