Results 1 to 6 of 6
  1. #1
    acev is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default create a program that will extract particular records from a file

    Hi All

    I am trying to create a program that will extract particular records from a file. The file is delimited by |.

    The working copy should enable this

    Java Code:
    C:\>java match "-t|" -4 -f Harvey -10 -f Atlanta callbook.txt
    "-t|" - denotes a delimiter
    -4 - denotes the field number
    -f - case sensitive

    Thanks

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Thumbs up regex

    There is a common tool implemented in java called regular expressions.
    java.util.regex (Java 2 Platform SE v1.4.2)
    Java Code:
    java.util.regex.Pattern.compile("-t|\\w{0,4} ..... and so on ....");// much work omitted
    To address individual fields in a line we would use a line reader, from which the returned line may be plaeced in an array using String.split(" ");

    case sensitive is the default.

    This only a partal solution, others will likely add to it.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    acev is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    Hi

    Thanks for your response.
    This is what I have done so far:

    Java Code:
     import  java.io.*;
    import java.util.*;
    
    public class assess1
    {
        static public void main(String args[])
        {
            int[] totals = new int[10];
            try
            {
                BufferedReader inFile = new BufferedReader(new FileReader(args[0]) ); 
                try
                {
                    String line;
    		String StrSearch = "HARVEY";
    		int counter = 0;
    
                    boolean foundIt = false;
    
                    while((line = inFile.readLine()) != null)
                    {
            	    // search for the string
    			
      	           String[] values = line.split("\\|");
            
                       for (String str : values) {
    			if (str.equals("HARVEY", CASE_SENSITIVE)) {
                     		System.out.println(line);
            	  		System.out.println();
    				counter++;
                             }
    		   }
    
                    }
    
    		if (counter == 0) {
                     		
    			System.out.println("String not found");
    		}
                    
    		inFile.close();
                }
                catch(IOException e)
                {
                    System.err.println("IO exception");
                    System.exit(1);
                }
                catch(NumberFormatException e)
                {
                    System.err.println("Value " + e.getMessage() + "not numeric");
                }
            }
            catch(FileNotFoundException e)
            {
                System.err.println( "Couldn't open " + e.getMessage() );
                System.exit(1);
            }
    
        }
    }
    Note the search string is hard coded and none of the other criterias have been implemented I am struggling.

    This code below I tried to use for reading the arguments from command line.

    Java Code:
    import  java.io.*;
    import java.util.*;
    
    public class check1
    {
        static public void main(String args[])
        {
            // Check if a command line argument exists
            if(args.length == 0)
                System.exit(0);
                
            // Loop through the command line arguments
    
    	String sSeperator;
    	String sPosition;
    	String sCase;
    	String sTranspo;
    	String sDiffer;
    	String filename;
    	String strSerach;
    	String vArgs;	
    
            for(int counter = 0; counter < args.length; counter++){
    		
    	    vArgs = args[counter];
    
    	    if (vArgs.substring(1,2) = "-t") {
    		sSeperator = vArgs;
    	    }
    	    
    
    	    if (vArgs.equals("-f")){
    		
    		sCase = vArgs;
    	    }
    
    	    if (vArgs.substring(vArgs.length -3, 3) = "txt") {
    		
    		filename = vArgs;
    	    }
    
                System.out.println("argument index : " + vArgs);   
                System.out.println();
                System.out.println(" first : " + sSeperator);    
                System.out.println(" Second : " + sPosition);    
                System.out.println(" third : " + sCase);    
                System.out.println(" fourth : " + sTranspo);    
                System.out.println(" fifth : " + sDiffer);    
                System.out.println(" sixth : " + filename); 
                System.out.println(" seventh : " + strsearch); 
    		
            }
        }
    }
    I would really appreciate it if you could tell me what I can do to solve this.

    Acev

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,573
    Rep Power
    25

    Default

    Comments on several parts of your program:
    Java Code:
       if(args.length == 0)
                System.exit(0);
    Put out an error message so user will know what happened.

    Java Code:
     if (vArgs.substring(1,2) = "-t") {
    What about the leading "?

    Use if( ) { ...}else if( ) {...} vs separate if statements. Otherwise all the ifs will be executed. It may be true that more than one if is true, but if not, chain the ifs with elses.

    what I can do to solve this.
    Write the code to solve the problem. Compile and test it. If the output is wrong, copy and paste it here and describe what's wrong with it and how you want it to look. Also comment the code to describe your logic and what it is you are trying to do so that someone reading the code can understand what you are trying to do. Most of us can see what the code is actually doing, but we don't know what you are trying to do.

  5. #5
    acev is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    Hi

    Thanks for your reply my question is how do I extract 2 different search criterias and use them to search the file? I have taken the If else if syntax on board I will change the code. Please advise.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,573
    Rep Power
    25

    Default

    how do I extract 2 different search criterias
    Extract from where?
    An example might help. Show the source and what you want extracted from it.

Similar Threads

  1. Extract A tar.gz file
    By sajdutt in forum Advanced Java
    Replies: 3
    Last Post: 03-03-2009, 09:29 AM
  2. To open an image file such as Jpeg file using JAva Program
    By itmani2020 in forum Advanced Java
    Replies: 10
    Last Post: 07-11-2008, 09:57 AM
  3. Any ideas to create a filter program?
    By paulachrist in forum New To Java
    Replies: 4
    Last Post: 07-11-2008, 03:53 AM
  4. [SOLVED] How to Extract Data From this text file?
    By jazz2k8 in forum New To Java
    Replies: 31
    Last Post: 04-18-2008, 10:45 AM
  5. Extract Text from PDF File using java
    By TSW1016 in forum Advanced Java
    Replies: 5
    Last Post: 01-06-2008, 11:03 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
  •