aspose file tools*
The moose likes Java in General and the fly likes modifying an input file based on pattern matching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "modifying an input file based on pattern matching" Watch "modifying an input file based on pattern matching" New topic
Author

modifying an input file based on pattern matching

Subhash Sriram
Greenhorn

Joined: Nov 10, 2003
Posts: 12
Hi,
I could REALLY use some help here..I have a program that is suppose to take in an input file, along with command line arguments, could be there, doesnt have to. The main function of the program is to check the input file for "tabs", and change all tabs to 3 spaces by default, or if the user specifies another amount, between 1 & 9, it is suppose to change it to that. Also, an optional [-d] option will take off all ^M's from the file (the result of windows --> unix ftp)...
I am using REGEX to check for a pattern, but I am lost as what to do when it finds a pattern, am also getting a couple of errors..Any help would be greatly appreciated.
Here is my code so far:

Thanks in advance.


Subhash
Angel Dobbs-Sciortino
Ranch Hand

Joined: Sep 10, 2003
Posts: 101
What kind of errors are you getting? Post what the screen shows.
Angel
Subhash Sriram
Greenhorn

Joined: Nov 10, 2003
Posts: 12
Hi,
Thank you for the reply..I modified the processTest function to the following. I found an article on how to do replacements using REGEX, but not when the INPUT is a file, that is what is really confusing me..
Below is the modified function:

And, this is the error that I am getting when running the file.
C:\cis3020\temp>java Homework -5 tester.java
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: -1
at java.lang.String.substring(Unknown Source)
at java.lang.String.subSequence(Unknown Source)
at java.util.regex.Matcher.getSubSequence(Unknown Source)
at java.util.regex.Matcher.appendReplacement(Unknown Source)
at Homework.processTest(Homework.java:90)
at Homework.main(Homework.java:52)
Also, I am not sure how to write the modified line back to the file. We are suppose to have a renameTo function which saves the inFile as a temp file, and then when the changes are made, it renames it back to the same as the input file...Would that have to be done first?
Thanks again
Subhash Sriram
Greenhorn

Joined: Nov 10, 2003
Posts: 12
Well, I have noticed a few problems with it right off the bat...I guess I am going back to the drawing board...
If anyone can give me some advice on how to tackle this problem, I would really appreciate it.
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
Unless this is part of your homework, your approach is very complex
A couple of points:
  • In appendReplacement the argument for replacement is not a regex.
  • Compile your REGEX only once. (not in a loop)
  • Consider using the replaceAll method of the String class.
  • Follow the coding conventions that you were taught.


  • I think that all you need is the repalceAll:


    Normal is in the eye of the beholder
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    You might also check your requirement. Is it really replace tab with "n" spaces or like a code editor insert enough spaces to get the next character up to 1 + a multiple of "n" columns from the left? In my favorite text editor tabs are set to 3. If I hit tab in column 2 it does not insert 3 spaces. It inserts 2 spaces to get me to column 4. For that I'd probably abandon a regex solution and read char by char. BTW: I did once in Pascal and there were some surprising gotchas. Maybe cause I was 20 years younger. Make yourself some good test cases!


    A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
    Subhash Sriram
    Greenhorn

    Joined: Nov 10, 2003
    Posts: 12
    Hi,
    Thank you all for the replies...
    I think I have a fairly ok idea of what to do, except for one thing.
    I am reading in a file one character (byte) at a time, and I need to compare each byte to the tab character to see if there is a tab, and then replace with either the default number of spaces or the user specified number of spaces, but I have no idea how to compare the two!
    BTW, if it helps, the assignment is shown here:
    http://www.cs.uwf.edu/~lwhite/course_pages/java-assignments/java11.htm
    Thanks again.
    [ November 26, 2003: Message edited by: Subhash Sriram ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: modifying an input file based on pattern matching
     
    Similar Threads
    Hot to deal with OutOfMemoryError
    Regex problem
    Ecryption program question
    Problem inputting and outputting strings correctly (weird error)
    Topic: Regex problem for pattern and grouping