Results 1 to 3 of 3
  1. #1
    MZA
    MZA is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default 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
    	}
    
    	
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default

    1.you put the comments on top of everymethod what exactly its going to do.Automtically you will get the code flow.
    2.whenever u open a file,u should have method to close.

    Do post the code with code tag so that we can check where you are going wrong.
    Ramya:cool:

Similar Threads

  1. search with part of string
    By virendra in forum Lucene
    Replies: 1
    Last Post: 01-21-2010, 12:56 PM
  2. Problem with empty search string
    By virendra in forum Lucene
    Replies: 0
    Last Post: 01-06-2010, 10:42 AM
  3. Search string for non-alphabetic characters
    By turnergirl24 in forum New To Java
    Replies: 5
    Last Post: 10-16-2009, 02:02 AM
  4. Search a string in a byte array
    By 2BOrNot2B in forum New To Java
    Replies: 0
    Last Post: 03-12-2009, 05:52 PM
  5. Replies: 0
    Last Post: 11-20-2007, 04:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •