简体   繁体   English

Java-用word.charAt(i + 1)替换word.charAt(i)并使word.charAt(i + 1)='A'

[英]Java - replacing word.charAt(i) with word.charAt(i+1) and making word.charAt(i+1) = 'A'

I need to look at each word in an array, so I made a for loop with array[i] equaling String word , then I made another for loop that has an if statement that is changing word.charAt(i) with word.charAt(i+1) and changing word.charAt(i+1) = 'A' . 我需要看看在阵列中的每个单词,所以做了一个for looparray[i]等于String word ,然后我又for loop具有一个if statement即改变word.charAt(i)word.charAt(i+1)并更改word.charAt(i+1) = 'A'

The objective is to 'encrypt' the word by checking if i is equal to 'A', and if it is then switching positions with i+1 . 目的是通过检查i是否等于'A',然后通过i+1切换位置来“加密”单词。

Here is my code: 这是我的代码:

for(int i = 0; i < unchangedFileInput.length; i++) {
    String word = unchangedFileInput[i];
    for(int v = 0; v < word.length();v++) {
        if (word.charAt(v) == 'A'
                && word.charAt(v+1) != 'A' 
                && word.length() > 1) {
            char[] mywordChars = word.toCharArray();
            temp = mywordChars[v+1];
            mywordChars[v] = mywordChars[v+1];
            mywordChars[v+1] = 'A';
            word = String.valueOf(mywordChars);
        } else {
            System.out.println(word);
        }
    }
    System.out.println(word);
}

unchangedFileInput is an array that has the values of: unlimitedFileInput是一个数组,其值为:

AVACADO
CHICKEN 
BACON 
AARDVARK

This is what the final result should look like: 最终结果应如下所示:

AVACADO = VACADAO 
CHICKEN = UNCHANGED 
BACON = BCAON 
AARDVARK = ARADVRAK 
NAAN = NANA

You need to check that you don't go past the array limit first. 您需要先检查是否没有超出数组限制。 I created a test framework, 我创建了一个测试框架,

public static void main(String[] args) {
    String[] unchangedFileInput = { "AVACADO", "CHICKEN", "BACON",
            "AARDVARK", "NAAN" };
    String[] changedFileInput = new String[unchangedFileInput.length];
    for (int i = 0; i < unchangedFileInput.length; i++) {
        changedFileInput[i] = transformName(unchangedFileInput[i]);
        System.out.printf("%s = %s%n", unchangedFileInput[i],
                changedFileInput[i]);
    }
}

And then wrapped your code in a method (and just created a new String with a StringBuilder removing the array conversions), 然后将您的代码包装到一个方法中(并使用StringBuilder创建了一个新的String ,删除了数组转换),

private static String transformName(String in) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0, len = in.length(); i < len; i++) {
        char ch = in.charAt(i);
        if (ch == 'A') {
            if (i + 1 < in.length() && in.charAt(i + 1) != 'A') {
                sb.append(in.charAt(i + 1));
                i++;
            }
        }
        sb.append(ch);
    }
    return sb.toString();
}

And I get your requested output, 我得到了您要求的输出,

AVACADO = VACADAO
CHICKEN = CHICKEN
BACON = BCAON
AARDVARK = ARADVRAK
NAAN = NANA

In order to achieve what you want you need to start the iteration from the end of the word to the beginning, otherwise, when you swap 'A' with the following character, you will have the 'A' again at the next iteration. 为了实现所需的功能,您需要从单词的末尾到开头开始迭代,否则,当将“ A”替换为以下字符时,在下一个迭代中将再次具有“ A”。 If you start from the end, you will hit each 'A' only once. 如果从头开始,则只会打一次每个“ A”。

Using this code 使用此代码

for(int v = 0; v < word.length();v++) {
                if (word.charAt(v) == 'A' && word.charAt(v+1)

You will get an IndexOutOfBoundsException at the end of the String. 您将在String的末尾获得IndexOutOfBoundsException。

And if you are talking about the IndexOutOfBoundsException error!! 如果您正在谈论IndexOutOfBoundsException错误!

I suggested, v < word.length() - 1; 我建议v < word.length() - 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM