views:

40

answers:

3

I'm making a console-based Java/Groovy application that does a lot of find/replaces in text files. If, say, the program knows you replaced foo with bar last time, it should by default know you probably want to replace the next foo with bar as well. My idea was to pre-populate the input field of the What would you like to rename this to? prompt so that you don't have to retype it unnecessarily, but I can't find an easy way to do this in Java.

Is this possible, and if so, would it be a recommended practice?

+1  A: 

Why don't you just assume that an empty imput is equal to the last input inserted? It would be quite easier to manage that pre-filling a stdin..

in any case if you plan to have a recent list I would suggest you to have a special character combination to tell last one or the previous and so on.

Jack
That's actually the current default behavior. I figured, however, it would be more efficient to pre-fill the input field than take up more space saying "Found Y, the previous value was X, press enter to use that."
+1  A: 

There are a lot of useful approaches to this problem. You can remember only the last value the user replaced, or you can mantain a cache with the last n replacements, in case the user requests a similar replacement. Or you can ignore the previous input and force the user to provide a replacement every time you need. The right approach depends on the frequency of the replacement operations, if the user replaces the same value very often, etc. You have to choose the right solution depending on your problem domain and on the kind of interaction you want to provide to the user.

Implementing these solutions is simple. The most intuitive is saving the replacement cache in a simple text file, loading it during startup and saving the cache at the shutdown. The replacement cache can also be serialized to the file, insted of being written as plain text.

frm
My problem, however, is not knowing how to go from, say, a map of previous matches->replacements but from finding a match and inserting the previous replacement into the input buffer. This would be especially useful if the new replacement is mostly the same but differs by a number appended to the end.
A: 

For reference, java.util.prefs.Preferences is available to command line programs, too.

trashgod