Results 1 to 6 of 6
- 07-10-2008, 10:58 PM #1
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
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
"-t|" - denotes a delimiterJava Code:C:\>java match "-t|" -4 -f Harvey -10 -f Atlanta callbook.txt
-4 - denotes the field number
-f - case sensitive
Thanks
- 07-10-2008, 11:27 PM #2
regex
There is a common tool implemented in java called regular expressions.
java.util.regex (Java 2 Platform SE v1.4.2)
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(" ");Java Code:java.util.regex.Pattern.compile("-t|\\w{0,4} ..... and so on ....");// much work omitted
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
- 07-10-2008, 11:36 PM #3
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
Hi
Thanks for your response.
This is what I have done so far:
Note the search string is hard coded and none of the other criterias have been implemented I am struggling.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); } } }
This code below I tried to use for reading the arguments from command line.
I would really appreciate it if you could tell me what I can do to solve this.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); } } }
Acev
- 07-11-2008, 12:10 AM #4
Comments on several parts of your program:
Put out an error message so user will know what happened.Java Code:if(args.length == 0) System.exit(0);
What about the leading "?Java Code:if (vArgs.substring(1,2) = "-t") {
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.
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.what I can do to solve this.
- 07-11-2008, 12:54 AM #5
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
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.
- 07-11-2008, 03:26 AM #6
Similar Threads
-
Extract A tar.gz file
By sajdutt in forum Advanced JavaReplies: 3Last Post: 03-03-2009, 09:29 AM -
To open an image file such as Jpeg file using JAva Program
By itmani2020 in forum Advanced JavaReplies: 10Last Post: 07-11-2008, 09:57 AM -
Any ideas to create a filter program?
By paulachrist in forum New To JavaReplies: 4Last Post: 07-11-2008, 03:53 AM -
[SOLVED] How to Extract Data From this text file?
By jazz2k8 in forum New To JavaReplies: 31Last Post: 04-18-2008, 10:45 AM -
Extract Text from PDF File using java
By TSW1016 in forum Advanced JavaReplies: 5Last Post: 01-06-2008, 11:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks