在 string:bound 中更改随机索引的元音必须为正

Change randomly indexed vowel in a string:bound must be positive

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(int) 时我很困惑为什么会这样。我从其他 Whosebug 问题中读到的是,这是为数组生成随机索引的有效方法。

我写了一个拼写检查器,想测试它的准确性,这是一个常见的错误,即更改单词的单个元音。我计划稍后从 运行 和 changevowel.txt 针对 dictionary.txt 创建的大列表中删除正确的单词,并从 changevowel.txt 中删除正确的单词,但这对眼前的问题。

试试这个逻辑:

  • 得到词外元音的所有索引
  • 选择一个随机的元音索引并从单词中取出它
  • 从 "vowels list" 中选择另一个与上一步不同的随机元音
  • 用新的随机元音替换所选的随机元音

示例:

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: %s\n", word);

    // Replace the vowel
    wordArray[randomVowelIndex] = replacementVowel;
    word = new String(wordArray);

    System.out.printf("After : %s\n", 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

Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive at java.util.Random.nextInt(Unknown Source)

不是来自这一行characters[vIndex[random2]] = vowels[random1];

但是这一行:

int random2 = (rand.nextInt(vIndex.length));

rand.nextInt的参数必须是> 0,但是vIndex.length == 0不知何故。