在 Java 控制台中预填充输入

发布于 2024-09-06 08:24:01 字数 310 浏览 1 评论 0原文

我正在制作一个基于控制台的 Java/Groovy 应用程序,它在文本文件中执行大量查找/替换操作。比如说,如果程序知道您上次将 foo 替换为 bar,那么默认情况下它应该知道您可能想要将下一个 foo 替换为也是如此。我的想法是预先填充 What would you like to rename this to? 提示的输入字段,这样您就不必不必要地重新输入它,但我找不到简单的方法在 Java 中执行此操作。

这可能吗?如果可以,这会是推荐的做法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小ぇ时光︴ 2024-09-13 08:24:01

为什么不假设空输入等于最后插入的输入?管理预填充 stdin 会更容易。

无论如何,如果您打算有一个最近的列表,我建议您使用特殊的字符组合来告诉最后一个或前一个等等。

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.

荭秂 2024-09-13 08:24:01

对于这个问题有很多有用的方法。您可以仅记住用户替换的最后一个值,或者您可以保留最后 n 个替换的缓存,以防用户请求类似的替换。或者您可以忽略之前的输入并强制用户在每次需要时提供替换。正确的方法取决于替换操作的频率、用户是否经常替换相同的值等。您必须根据您的问题域以及您想要向用户提供的交互类型来选择正确的解决方案。

实施这些解决方案很简单。最直观的是将替换缓存保存在一个简单的文本文件中,在启动时加载它并在关闭时保存缓存。替换缓存也可以序列化到文件中,而不是写入为纯文本。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文