简体   繁体   English

如何替换数组中的字符串字符

[英]How to replace string characters from an array

I'm trying to create an encryption scheme that replaces the letters in a String with a corresponding letter. 我正在尝试创建一种加密方案,将String的字母替换为相应的字母。

For example, in the string "apple", the "a" is replaced with "k", and so on. 例如,在字符串“ apple”中,“ a”被替换为“ k”,依此类推。 Each letter has a fixed corresponding letter. 每个字母都有一个固定的对应字母。

I want to get user input and store it into an array. 我想获取用户输入并将其存储到数组中。 Then I want to loop through the array and find each index of the String . 然后,我想遍历数组并找到String每个索引。 Then replace each index with the corresponding letter. 然后用相应的字母替换每个索引。

Here's what I cooked up so far but I'm unable to make the code run. 这是我到目前为止编写的内容,但无法运行代码。 I'm mainly getting, error: incompatible types . 我主要是得到error: incompatible types

I can't determine whether I should be using the charAt method and changing my variable types to char. 我无法确定是否应该使用charAt方法并将变量类型更改为char。

import java.util.*;

public class Encrypt {

    public static void main(String [] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the String Name to encrypt:");
        String inputString = input.nextLine();
        String[] str = new String[inputString];
        replaceString();
    }

    public static void replaceString() {

        for(int i = 0; i < str.length(); i++) {
            if(str.indexOf(i) == "a") {
                str.indexOf(i) = "k";
            } else if(str.indexOf(i) == "b") {
                str.indexOf(i) = "n";
            }
            //and so on A-Z...
            System.out.print(str);
        }

    }
}

Why do you want to use String[] ? 为什么要使用String[] IMO you should use char[] and the following code will do what you want: IMO,您应该使用char[] ,以下代码将完成您想要的操作:

public static void main(String[] args) throws IOException {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the String Name to encrypt:");
    String inputString = input.nextLine();
    char[] str = inputString.toCharArray();
    replaceString(str);
}

public static void replaceString(char[] str) {
    int length = str.length;
    for(int i = 0; i < length; i++) {
        if(str[i] == 'a') {
            str[i] = 'k';
        } else if(str[i] == 'b') {
            str[i] = 'n';
        }
    }
        System.out.println(Arrays.toString(str));
        System.out.println(String.valueOf(str));
}

Running the program: 运行程序:

Enter the String Name to encrypt: bat 输入要加密的字符串名称:bat
[n, k, t] nkt [n,k,t] nkt

You're looking for String#replace(char oldChar, char newChar) 您正在寻找String#replace(char oldChar,char newChar)

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. 返回一个新字符串,该字符串是用newChar替换此字符串中所有出现的oldChar的结果。

Your code would have to loop a pre-determined amount of times and store each String in the array 您的代码将必须循环预定的时间并将每个String存储在数组中

Scanner input = new Scanner(System.in);
String[] str = new String[5];
for (int i = 0; i < 5; i++){
    System.out.println("Enter the String Name to encrypt:");
    String inputString = input.nextLine();
    str[i] = inputString.replace('a', 'k');
}
for (String s : str){
    System.out.println("Encrypted word: " + s);
}

NOTE 注意

Your previous way of declaring an array, 您以前声明数组的方式,

String[] str = new String[inputString];

is incorrect. 是不正确的。 Look here to practice more with arrays. 请看这里,以更多地练习数组。 Essentially, the formula is: 本质上,公式为:

Type[] myArr = new Type[sizeOfArray];

Problem is String.indexOf() gives you index of first occurence. 问题是String.indexOf()提供了第一次出现的索引。 It won't replace the string. 它不会替换字符串。

str.indexOf(i) = "k"; //won't replace anything here

BTW, there are several other syntax errors in your code. 顺便说一句,您的代码中还有其他语法错误。

Try this example, if you want use loops: 如果要使用循环,请尝试以下示例:

 import java.util.Scanner; class Ideone { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter the String Name to encrypt:"); String str = input.nextLine(); input.close(); str = replaceString(str); // note, self assignment System.out.println(str); } public static String replaceString(String str) { char[] tmp = str.toCharArray();// get all into array for (int i = 0; tmp.length > i; i++) { if ('a' == tmp[i]) { tmp[i] = 'k'; } else if ('b' == tmp[i]) { tmp[i] = 'n'; } } return new String(tmp); //create new string with modified array } } 

Run code here 在这里运行代码

As suggested by others, you may also look into String.replaceAll() : 正如其他人所建议的,您也可以查看String.replaceAll()

 import java.util.Scanner; class Ideone { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter the String Name to encrypt:"); String str = input.nextLine(); input.close(); str = replaceString(str); System.out.println(str); } public static String replaceString(String str) { return str.replaceAll("a", "k").replaceAll("b", "n"); } } 

Run code here 在这里运行代码

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

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