java - 用arraylist中的单词替换txt文件中的单词?
问题描述
我有一个程序询问用户问题并将它们存储到数组列表中。我想获取我的文本文件并将以 ] 结尾的单词替换为我之前的 arraylist 中的单词。这只是我的一段代码,我在其中扫描文本文件并找到以 ] 结尾的单词并打印它们。(对不起,如果我的代码有点混乱,我对 java 还是有点陌生。)
//Scanning the chosen story file for story and custom words
Scanner sf = new Scanner(new File("vacation.txt"));
while (sf.hasNextLine()) {
String current = sf.nextLine();
String all_words[];
all_words = current.split(" ");
List<String> mywordList = new ArrayList<String>(all_words.length);
for (String s:all_words) {
mywordList.add( s );
}
for (String s : mywordList)
{
if (s.endsWith("]"))
System.out.print( s + " " );
解决方案
推荐阅读
- python - 匹配字符串中的项目
- sql - Crystal Reports 一对多结果过滤
- crystal-lang - 空集文字语法
- sql - 从 AS400 提取数据时如何显示列标题和数据
- swift - Cocoa Swift:将多个 NSImage 文件保存到选定的 url 或路径与沙箱
- angular - Angular 测试用例 Jasmine
- ruby-on-rails - 在不中断请求的情况下从 Rails 重新加载 Nginx
- php - php pdo 仅在给定值的情况下准备 exec 语句
- javascript - 尝试使用 css(活动)和 javascript 在点击时显示内容。如何保持内容显示?
- angular - 调整 mat-option 元素的宽度(更短)