Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

modifying an input file based on pattern matching

 
Subhash Sriram
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Angel Dobbs-Sciortino
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of errors are you getting? Post what the screen shows.
Angel
 
Subhash Sriram
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
     
    Stan James
    (instanceof Sidekick)
    Ranch Hand
    Posts: 8791
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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!
     
    Subhash Sriram
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic