0

試みているプロジェクトの特定のキーワードの検索について学習しようとしています。他の人が作成したプログラムを使用しており、2 つの引数を受け入れるように変更しようとしました。これまでのところ、「検索中」 + args[0] の後に args[1] を追加しようとしましたが、2 番目の引数が検索されることを期待していましたが、これは機能せず、位置が見つかったとプログラムが報告するようになりました - 2 行目の 1。読み取っているテキスト ファイルは次のように表示されます。

one
two
three

2 つの引数を渡す方法を教えてください。

ありがとうございました

// 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());
    }
}

}

4

1 に答える 1

1
// 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());
    }
}

}

検索が行われるコメントで述べました。そのほとんどをコピーして、args[1] に対してもう一度行うことができると思います。

より多くの引数について(それが最善/最速の解決策であるかどうかはわかりません):

// 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());
    }
}

}

これが機能するかどうかはわかりませんが、おそらくそうではないかもしれませんが、これは頭の中で書いたものです(テストされていません)。

于 2012-05-30T14:41:55.373 に答える