【问题标题】:Iterate through List of single character strings using indexOf()使用 indexOf() 遍历单个字符串列表
【发布时间】:2014-10-01 22:02:38
【问题描述】:

我正在制作一个程序,它将接受一个输入字符串并使用 Rot13 加密方法对其进行解码。这需要字母表,并将其旋转 13。

我很难获得列表中字母的索引,每次运行它都会给我-1,就好像该项目不在列表中一样。我查看了 java 文档, indexOf() 要求一个对象。我尝试将我的输入显式输入为对象,但这也不起作用。

这是我到目前为止的代码:

package rot13;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author andrewjohnson
 */
public class CipherKey {

    List<String> alpha = Arrays.asList("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", " ");
    List<String> alphaRev = Arrays.asList("Z", "Y", "X", "W", "V", "U", "T", "S", "R", "Q", "P", "O", "N", "M", "L", "K", "J", "I", "H", "G", "F", "E", "D", "C", "B", "A", " ");

    public String codeDecode(String s) {
        System.out.println(s);

        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            //System.out.println(ch);
            int x = alpha.indexOf(ch);
            //System.out.println(x);
            String y = alphaRev.get(x);
            System.out.print(y);
        }

        return null;
    }

    public static String readInput() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter message to be encoded or decoded");

        String s = br.readLine().toUpperCase();
        //System.out.println(s);

        return s;

    }
}

还有我的 main():

/**
 *
 * @author andrewjohnson
 */
public class Rot13 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        CipherKey x = new CipherKey();


        x.codeDecode(x.readInput());
    }

}

我不确定为什么它不起作用,但我已将其范围缩小到以下范围:

 int x = alpha.indexOf(ch);

无法在 alpha 中找到 ch。我是 Java 新手,我已经尝试了所有我能想到的东西。感谢您的建议!

【问题讨论】:

    标签: java list encryption indexof


    【解决方案1】:

    问题出在这里:

    char ch = s.charAt(i);
    
    int x = alpha.indexOf(ch); // <-------- HERE
    

    您正在String 数组中搜索char。这当然不存在。亨斯-1

    改成int x = alpha.indexOf("" + ch);

    int x = alpha.indexOf(Character.toString(ch));

    int x = alpha.indexOf(String.valueOf(ch));

    这些都可以。

    【讨论】:

    • 这就像一个冠军!谢谢!我的程序说:GSZG DLIPVW ORPV Z XSZNK! GSZMP BLF!我在 where if x == -1 中添加了一个 if 语句来打印 ch,因此它说明了不在列表中的标点符号。
    • @AndyJohnson 请注意,执行列表线性搜索(使用.indexOf)以查找匹配字符的算法效率非常。这个答案解决了眼前的问题,但不是算法。
    • 我知道。最好只使用一个数组和一些数学来找到 13 个位置之外的旋转索引。这只是一个教我Java的练习。我会继续努力的。
    【解决方案2】:

    ch 的类型为 char,您的列表包含 String。对于indexOf,列表可以接受Object,但类型仍然很重要。

    int x = alpha.indexOf(ch); 更改为 int x = alpha.indexOf(String.valueOf(ch)); 以修复它。

    例子:

    System.out.println(alpha.indexOf('D'));
    System.out.println(alpha.indexOf(String.valueOf('D')));
    

    将打印

    -1
    3
    

    【讨论】:

      【解决方案3】:
      1. 这不是 rot13 算法 - 你只是似乎在颠倒字母表。 rot13 将范围A - M 映射到N - Z,反之亦然。两次调用 rot13 会返回原始文本。

      2. ASCII 字母遵循数字序列。与通过列表执行线性搜索来查找匹配索引不同,只计算当前字母与A 之间的差异,然后使用该差异偏移到第二个数组中会更快字符数(或字符串)。

      static String map = "NOPQRSTUVWXYZABCDEFGHIJKLM";  // for rot13
      
      function rot13(String input) {
          StringBuffer output;
          for (char ch : input) {
              int index = ch - 'A';
              if (ch >= 0 && ch < 26) {
                  output.append(map.charAt(index));
              } else {
                  output.append(ch);
              }
          }
          return output.toString();
      }
      

      注意:未经测试,可能无法编译,E&OE 等

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-02
        • 2014-04-14
        • 2014-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多