Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取文件并提供多个要搜索的参数_Java_Search_Arguments - Fatal编程技术网

Java 读取文件并提供多个要搜索的参数

Java 读取文件并提供多个要搜索的参数,java,search,arguments,Java,Search,Arguments,我正在尝试学习如何为我正在尝试的项目搜索某些关键字。我正在使用一个由其他人创建的程序,我试图修改它以接受两个参数。到目前为止,我尝试在“搜索”+args[0]之后添加args[1],希望它能搜索第二个参数,但这不起作用,导致程序报告该位置位于第2行的-1处。我正在读取的文本文件显示为: one two three 有人能帮我传递两个论点吗 多谢各位 // Import io so we can use file objects import java.io.*; public class s

我正在尝试学习如何为我正在尝试的项目搜索某些关键字。我正在使用一个由其他人创建的程序,我试图修改它以接受两个参数。到目前为止,我尝试在“搜索”+args[0]之后添加args[1],希望它能搜索第二个参数,但这不起作用,导致程序报告该位置位于第2行的-1处。我正在读取的文本文件显示为:

one
two
three
有人能帮我传递两个论点吗

多谢各位

// Import io so we can use file objects
import java.io.*;

public class searchfile {
public static void main(String args[]) {
    try {
        // Open the file c:\test.txt as a buffered reader
        BufferedReader bf = new BufferedReader(new FileReader("C:/Users/Sean/Desktop/Java/MyText.txt"));

        // Start a line count and declare a string to hold our current line.
        int linecount = 0;
            String line;

        // Let the user know what we are searching for
        System.out.println("Searching for " + args[0] + " in file...");

        // Loop through each line, stashing the line into our line variable.
        while (( line = bf.readLine()) != null)
        {
                // Increment the count and find the index of the word
                linecount++;
                int indexfound = line.indexOf(args[0]);

                // If greater than -1, means we found the word
                if (indexfound > -1) {
                     System.out.println("Word was found at position " + indexfound +" on line " + linecount);
                }
        }

        // Close the file after done searching
        bf.close();
    }
    catch (IOException e) {
        System.out.println("IO Error Occurred: " + e.toString());
    }
}
}

我在评论中说搜索是在哪里完成的。我认为您可以复制其中的大部分内容,并为args[1]再次执行该操作

有关更多参数(不确定这是否是最佳/最快的解决方案):


我不确定这是否有效,可能不行,但我只是在脑子里写的(未经测试)。

您真的只是更改了初始的
System.out.println
调用吗?你现在对代码的工作原理了解多少?虽然不是你具体问题的答案,但如果你对IO和解析感兴趣,这会有所帮助。你发布的代码甚至没有尝试使用第二个参数。Jon Skeet-不,我没有。在int indexfound=line.indexOf(args[0])行下面;我添加了int indexfound=line.indexOf(args[1]);我知道参数[0]是第一个参数,并认为只需添加参数[1]就可以搜索第二个参数。David B-谢谢,我一直在研究这个问题。是否可以使用正则表达式作为参数?它正在工作,感谢您的帮助。是否有一种方法可以接受多个参数而不在每个参数中进行编码。假设我想搜索100个不同的参数,那么我必须向上搜索(args[99]);看看循环!您可以使用一个名为index的变量,然后对
args[index]
执行此操作。
// Import io so we can use file objects
import java.io.*;

public class searchfile {
public static void main(String args[]) {
    try {
        // Open the file c:\test.txt as a buffered reader
        BufferedReader bf = new BufferedReader(new FileReader("C:/Users/Sean/Desktop/Java/MyText.txt"));

        // Start a line count and declare a string to hold our current line.
        int linecount = 0;
            String line;

        // Let the user know what we are searching for
        // i.e. just printing to the console, not actually searching.
        System.out.println("Searching for " + args[0] + " in file...");

        // Loop through each line, stashing the line into our line variable.
        while (( line = bf.readLine()) != null)
        {
                // Count lines. We read a line this variable (linecount) increments by one
                linecount++; 
                // the indexOf function returns the index (i.e. the place of) the parameter, in this case args[0]. If it doesn't find the parameter, it returns -1, an impossible value, so we know it wasn't found.
                // Here is the actual searching done, till...
                int indexfound = line.indexOf(args[0]);

                // If greater than -1, means we found the word
                if (indexfound > -1) {
                     System.out.println("Word was found at position " + indexfound +" on line " + linecount);
                }
                // ...here.
        }

        // Close the file after done searching
        bf.close();
    }
    catch (IOException e) {
        System.out.println("IO Error Occurred: " + e.toString());
    }
}

}
// Import io so we can use file objects
import java.io.*;

public class searchfile {
public static void main(String args[]) {
    try {
        // Open the file c:\test.txt as a buffered reader
        BufferedReader bf = new BufferedReader(new FileReader("C:/Users/Sean/Desktop/Java/MyText.txt"));

        // Start a line count and declare a string to hold our current line.
        int linecount = 0;
            String line;

        // Let the user know what we are searching for
        // i.e. just printing to the console, not actually searching.
        System.out.println("Searching for " + args[0] + " in file...");

        // Loop through each line, stashing the line into our line variable.
        while (( line = bf.readLine()) != null)
        {
                // Count lines. We read a line this variable (linecount) increments by one
                linecount++; 
                // the indexOf function returns the index (i.e. the place of) the parameter, in this case args[0]. If it doesn't find the parameter, it returns -1, an impossible value, so we know it wasn't found.
                // Here is the actual searching done, till...
                for(int index = 0; true; index++) {
                try {
                int indexfound = line.indexOf(args[index]);

                // If greater than -1, means we found the word
                if (indexfound > -1) {
                     System.out.println("Word was found at position " + indexfound +" on line " + linecount);
                }
                }
                catch(ArrayIndexOutOfBoundsException ex) {
                    break;
                }
                // ...here.
                }
        }

        // Close the file after done searching
        bf.close();
    }
    catch (IOException e) {
        System.out.println("IO Error Occurred: " + e.toString());
    }
}

}