用另一个 ArrayList 中的随机字符串替换 ArrayList 中的特定字符串
Replace specific string in ArrayList with random string from another ArrayList
我是编程新手 java(几个月前才开始),不幸的是,我现在有点困惑。
我目前正在使用 BlueJ 进行编程,我在我的书中遇到了一个练习,我在掌握如何进一步添加所需功能方面确实遇到了一些问题。
该练习有两个以前制作的 classes(ReaderInput 和 WriterOutput),我将创建一个新的 class,它调用并使用前面的 classes 方法.练习包括两个 .txt 文件,一个包含故事,第二个包含不同种类的形容词(单词)。然后,我必须读取 story.txt 文件,并将该文件中的字符串替换为特定值(值为 "ADJECTIVE" 的字符串),以及从 adjectives.txt 文件中随机选择的单词,最后,将它们输出(写入)到一个全新的文件中。
来自 story.txt 文件的示例:
'Once upon a time there was a ADJECTIVE girl who were in a ADJECTIVE birthday togheter with lots of ADJECTIVE friends etc....'
来自 adjectives.txt 文件的示例:
发牢骚
老的
小的
愚蠢的
等...
所以,我创建了一个新的 class,添加并初始化了 ReaderInput class 的新字段对象,就像这样;
private ReaderInput reader;
private WriterOutput writer;
private Random random;
public StoryCreator()
{
reader = new ReaderInput();
writer = new WriterOutput();
random = new Random();
}
然后我创建了一个名为 createAdjectiveStory 的新方法,其参数用于指定故事文件名和形容词。
public void createAdjectiveStory(String storyFilename, String adjectivesFilename/*, String outputFilename*/)
然后,我声明两个变量来保存来自 ReaderInput class 的 reader 个单词(通过参数指定要读取的文件):
ArrayList<String> story = reader.getWordsInFile(storyFilename);
ArrayList<String> adjective = reader.getWordsInFile(adjectivesFilename);
那么最头疼的事情来了。同时,我已经能够声明一个字符串变量 wordToChange = "ADJECTIVE"; ,创建一个 for-each 循环来迭代故事数组列表集合,使用 if 语句检查元素中的字符串是否等于 wordToChange。最后我做了一个随机的通过形容词 arryalist,并从集合中得到一个随机词(每次我调用该方法并单独打印它时,这从文件中获取一个随机词)
for(String reader : story){
int index = random.nextInt(adjective.size());
String word = adjective.get(index);
if(reader.equals(wordToChange))
Collections.replaceAll(story,wordToChange,word);
}
}
在 if 语句中,我尝试使用 replaceAll 函数,如下所示:
Collections.replaceAll(story,wordToChange,word);
这里的问题是,如果我尝试打印出来(只是为了验证它是否有效),我得到这个:
'Once upon a time there was a OLD girl who were in a OLD birthday togheter with lots of OLD friends etc....'
现在我更想要这个。
'Once upon a time there was a OLD girl who were in a WHINY borthday togheter with lots of STUPID friends etc....'
我试过使用 .replace 函数,像这样:
Collections.replace(story,wordToChange,word);
但我收到一条错误信息:'Cannot find symbol - method replace'。
我在这里被困住了,我似乎找不到合适的解决方案。有人对此有任何 tips/tricks 或解决方案吗?
新程序员需要一点 "push" 正确的方向。任何帮助将不胜感激:)
问题是它选择单词将其更改为一次,然后 replaceAll 立即替换 REPLACE 的每个实例。
您应该做的是在 for 循环中使用迭代器:
Iterator<String> i=story.iterator();
while (i.hasNext()) {
String check = i.next();
if (check.equals(stringToReplace)) {
i.set(getRandomAdjective());
}
}
您应该为代码创建一个方法来获取随机形容词。
我是编程新手 java(几个月前才开始),不幸的是,我现在有点困惑。
我目前正在使用 BlueJ 进行编程,我在我的书中遇到了一个练习,我在掌握如何进一步添加所需功能方面确实遇到了一些问题。
该练习有两个以前制作的 classes(ReaderInput 和 WriterOutput),我将创建一个新的 class,它调用并使用前面的 classes 方法.练习包括两个 .txt 文件,一个包含故事,第二个包含不同种类的形容词(单词)。然后,我必须读取 story.txt 文件,并将该文件中的字符串替换为特定值(值为 "ADJECTIVE" 的字符串),以及从 adjectives.txt 文件中随机选择的单词,最后,将它们输出(写入)到一个全新的文件中。
来自 story.txt 文件的示例:
'Once upon a time there was a ADJECTIVE girl who were in a ADJECTIVE birthday togheter with lots of ADJECTIVE friends etc....'
来自 adjectives.txt 文件的示例:
发牢骚 老的 小的 愚蠢的 等...
所以,我创建了一个新的 class,添加并初始化了 ReaderInput class 的新字段对象,就像这样;
private ReaderInput reader;
private WriterOutput writer;
private Random random;
public StoryCreator()
{
reader = new ReaderInput();
writer = new WriterOutput();
random = new Random();
}
然后我创建了一个名为 createAdjectiveStory 的新方法,其参数用于指定故事文件名和形容词。
public void createAdjectiveStory(String storyFilename, String adjectivesFilename/*, String outputFilename*/)
然后,我声明两个变量来保存来自 ReaderInput class 的 reader 个单词(通过参数指定要读取的文件):
ArrayList<String> story = reader.getWordsInFile(storyFilename);
ArrayList<String> adjective = reader.getWordsInFile(adjectivesFilename);
那么最头疼的事情来了。同时,我已经能够声明一个字符串变量 wordToChange = "ADJECTIVE"; ,创建一个 for-each 循环来迭代故事数组列表集合,使用 if 语句检查元素中的字符串是否等于 wordToChange。最后我做了一个随机的通过形容词 arryalist,并从集合中得到一个随机词(每次我调用该方法并单独打印它时,这从文件中获取一个随机词)
for(String reader : story){
int index = random.nextInt(adjective.size());
String word = adjective.get(index);
if(reader.equals(wordToChange))
Collections.replaceAll(story,wordToChange,word);
}
}
在 if 语句中,我尝试使用 replaceAll 函数,如下所示:
Collections.replaceAll(story,wordToChange,word);
这里的问题是,如果我尝试打印出来(只是为了验证它是否有效),我得到这个:
'Once upon a time there was a OLD girl who were in a OLD birthday togheter with lots of OLD friends etc....'
现在我更想要这个。
'Once upon a time there was a OLD girl who were in a WHINY borthday togheter with lots of STUPID friends etc....'
我试过使用 .replace 函数,像这样:
Collections.replace(story,wordToChange,word);
但我收到一条错误信息:'Cannot find symbol - method replace'。
我在这里被困住了,我似乎找不到合适的解决方案。有人对此有任何 tips/tricks 或解决方案吗?
新程序员需要一点 "push" 正确的方向。任何帮助将不胜感激:)
问题是它选择单词将其更改为一次,然后 replaceAll 立即替换 REPLACE 的每个实例。
您应该做的是在 for 循环中使用迭代器:
Iterator<String> i=story.iterator();
while (i.hasNext()) {
String check = i.next();
if (check.equals(stringToReplace)) {
i.set(getRandomAdjective());
}
}
您应该为代码创建一个方法来获取随机形容词。