import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
public class MachinePedagogy {
public static void main(String[] args) throws IOException {
changeVowel("dictionary.txt");
}
private static void changeVowel(String path) throws IOException {
char ch;
BufferedWriter bWriter = new BufferedWriter(new FileWriter(new File("changevowel.txt")));
String aeiou = "aeiou";
char[] vowels = aeiou.toCharArray();
BufferedReader bReader = new BufferedReader(new FileReader(new File(path)));
for(String temp = bReader.readLine(); temp != null; temp = bReader.readLine()){
int counter = 0;
char[] characters = temp.toCharArray();
for(int i = 0; i < temp.length(); i++){
ch = temp.charAt(i);
if(
ch == 'a' ||
ch == 'e' ||
ch == 'i' ||
ch == 'o' ||
ch == 'u'){
counter++;
}
}
Random rand = new Random();
int vIndex[] = new int[counter];
int index = 0;
for (int j = 0; j <temp.length(); j++){
ch = temp.charAt(j);
if(
ch == 'a' ||
ch == 'e' ||
ch == 'i' ||
ch == 'o' ||
ch == 'u'){
vIndex[index] = j;
index++;
}
}
int random2 = (rand.nextInt(vIndex.length));
int random1 = (rand.nextInt(vowels.length));
characters[vIndex[random2]] = vowels[random1];
temp = String.valueOf(characters);
bWriter.write(temp + "n");
}
bReader.close();
bWriter.close();
}
}
为什么在这些数组上我的界有时是负的?
Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
at java.util.Random.nextInt(Unknown Source)
at MachinePedagogy.changeVowel(MachinePedagogy.java:56)
at MachinePedagogy.main(MachinePedagogy.java:14)
第56行:
characters[vIndex[random2]] = vowels[random1];
第14行:
changeVowel("dictionary.txt");
我想编辑字符串中的随机元音,并将其更改为另一个随机元音,最好与以前不同。Dictionary.txt只是一本斯坦福字典,如果你需要它来编译代码的话。
我发现的许多替换元音的例子都遵循这个逻辑。
temp.replaceAll( "[aeiou]", "?" );
我不想替换所有的元音,只想替换一个随机索引的元音。我认为这可能与nextInt((有关,但当我阅读文档Random#nextInt时,我很困惑为什么会这样。我从其他StackOverflow问题中了解到,这是为数组生成随机索引的有效方法。
我写了一个拼写检查器,想用一个常见的更改单词单个元音的错误来测试它的准确性。我计划稍后通过对dictionary.txt运行changevowel.txt来从我创建的大列表中删除正确的单词&从changevowel.txt中删除正确的单词,但这对眼前的问题并不重要
线程"main"java.lang.IollegalArgumentException中的异常:绑定必须为正位于java.util.Nrandom.nextInt(未知源(
不是来自此行characters[vIndex[random2]] = vowels[random1];
但这条线:
int random2 = (rand.nextInt(vIndex.length));
rand.nextInt
的参数必须是> 0
,但不知何故是vIndex.length == 0
。
试试这个逻辑:
- 从单词中取出元音的所有索引
- 随机选择一个元音索引并将其从单词中删除
- 从"元音列表"中选择另一个与上一步不同的随机元音
- 用新的随机元音替换所选的随机元音
示例:
static String vowels = "AEIOU";
public static void main(String[] args) throws Exception {
String word = "MIRACULOUSNESS";
char[] wordArray = word.toCharArray();
Random rand = new Random();
List<Integer> vowelIndexes = getVowelIndexes(word);
// Choose random vowel index and get the random vowel from the word
int randomVowelIndex = vowelIndexes.get(rand.nextInt(vowelIndexes.size()));
char randomVowel = word.charAt(randomVowelIndex);
// Choose random vowel replacement that isn't the same as the randomVowel
char replacementVowel = randomVowel;
while (replacementVowel == randomVowel) {
replacementVowel = vowels.charAt(rand.nextInt(vowels.length()));
}
System.out.printf("Before: %sn", word);
// Replace the vowel
wordArray[randomVowelIndex] = replacementVowel;
word = new String(wordArray);
System.out.printf("After : %sn", word);
}
public static List<Integer> getVowelIndexes(String word) {
List<Integer> result = new ArrayList();
for (int i = 0; i < word.length(); i++) {
if (vowels.contains(""+word.charAt(i))) {
result.add(i);
}
}
return result;
}
结果(每次运行各不相同(:
Before: MIRACULOUSNESS
After : MIRECULOUSNESS