• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

modifying an input file based on pattern matching

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
     
    (instanceof Sidekick)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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 ]
     
    Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic