Results 1 to 3 of 3
- 02-03-2010, 05:40 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 5
- Rep Power
- 0
Using arguments to search for a string
Hey,
I'm trying to create a program that takes a string and different text files in the command line arguments and then searches those files for said string and returns all text lines containing it. A sample command line is:
java Find [string] <file.ext> <file.ext>
The code I have so far is below. I honestly am completely stuck in terms of putting more code in to even search for the files using the args. Any help is appreciated.
Java Code:/** * @author: * * Purpose: This program takes command line arguments, uses the first argument as a pattern string, and the rest of the arguments as * the names of files to search for this pattern string. For each instance of the pattern found, the program will print out the name * of the file in which it was found, followed by a colon (:), followed by the full line of text containing the pattern. * */ public class Find { private static final int EXIT_NO_ARGS = 0; private static final int EXIT_MISSING_ARGS = 1; private static String pattern = ""; /** * @param args - interpreted as follows: * args[0] - a search pattern (string) * args[1] - the name of a text file to search * ... * args[n] - the name of a text file to search * * If no command line parameters are supplied, or if args[1] is missing, the program will prompt the user for the missing * parameters. */ public static void main(String[] args) { // Check arguments checkArgs(args); // Identify the pattern to search for pattern = getPattern(); // Open the search files openFile(); // Search the files and print the results to the screen searchFile(); } private static void checkArgs(String[] args) { // if (args.length < 1) { System.out.println("Usage: java Find [keyword] <file.extension> "); System.exit(0); } } private static void openFile() { // TODO Auto-generated method stub } private static String getPattern() { // TODO Supply to necessary code to implement this method return pattern; // This return statement will probably be changed } }
- 02-03-2010, 05:51 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
You might want to start by completing the checkArgs() method since that seems like a nice easy one.
Do you have documentation/instructions saying what that method should do?
If I were guessing I would say:
(1) You should replace 0 with the corresponding static final int value. It's good style to use such a value.
(2) You should complete the method so that it detects and returns the other exit value if there are arguments but not enough of them.
- 02-03-2010, 09:22 AM #3
Similar Threads
-
search with part of string
By virendra in forum LuceneReplies: 1Last Post: 01-21-2010, 12:56 PM -
Problem with empty search string
By virendra in forum LuceneReplies: 0Last Post: 01-06-2010, 10:42 AM -
Search string for non-alphabetic characters
By turnergirl24 in forum New To JavaReplies: 5Last Post: 10-16-2009, 02:02 AM -
Search a string in a byte array
By 2BOrNot2B in forum New To JavaReplies: 0Last Post: 03-12-2009, 05:52 PM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks