【问题标题】:How to replace string characters from an array如何替换数组中的字符串字符
【发布时间】:2015-12-25 06:01:14
【问题描述】:

我正在尝试创建一个加密方案,将String 中的字母替换为相应的字母。

例如,在字符串“apple”中,将“a”替换为“k”,以此类推。每个字母都有一个固定的对应字母。

我想获取用户输入并将其存储到数组中。 然后我想遍历数组并找到String 的每个索引。然后用相应的字母替换每个索引。

这是我到目前为止所做的,但我无法让代码运行。我主要是得到error: incompatible types

我无法确定是否应该使用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);
        }

    }
}

【问题讨论】:

  • str.indexOf(i) 返回int,而不是String,您可以使用str.charAt,它将返回char,这可能更实用。 == 不是你在 Java 中比较 Strings 的方式
  • 您不能将字符串传递给 []。

标签: java arrays string loops indexof


【解决方案1】:

您正在寻找String#replace(char oldChar, char newChar)

返回一个新字符串,该字符串是用 newChar 替换此字符串中所有出现的 oldChar。

您的代码必须循环预先确定的次数并将每个 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);
}

注意

你之前声明数组的方式,

String[] str = new String[inputString];

不正确。看看here 来练习更多的数组。本质上,公式是:

Type[] myArr = new Type[sizeOfArray];

【讨论】:

    【解决方案2】:

    为什么要使用String[]? 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));
    }
    

    运行程序:

    输入要加密的字符串名称:bat
    [n, k, t] nkt

    【讨论】:

    • 我使用 String[] 因为缺乏更好的理解。无论如何谢谢你。这就是我要找的。现在我必须弄清楚如何在没有逗号分隔的情况下加入字符。
    • @Almac:检查我的编辑,因为它完全解决了您的问题。
    【解决方案3】:

    问题是String.indexOf() 为您提供首次出现的索引。它不会替换字符串。

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

    顺便说一句,您的代码中还有其他几个语法错误。

    试试这个例子,如果你想使用循环:

    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
      }
    }

    运行代码here

    根据其他人的建议,您也可以查看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");
      }
    }

    运行代码here

    【讨论】:

      猜你喜欢
      • 2010-09-28
      • 1970-01-01
      • 2012-04-26
      • 2013-11-08
      • 2014-03-21
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      相关资源
      最近更新 更多