【问题标题】:converting a string array with letters into an int array in java在java中将带有字母的字符串数组转换为int数组
【发布时间】:2012-11-22 23:32:06
【问题描述】:

好的,我将在这里尝试解释我的问题,我需要做的是将字符串数组转换为 int 数组。

这是我所拥有的一部分(以及初始设置)

   System.out.println("Please enter a 4 digit number to be converted to decimal ");
    basenumber = input.next();

    temp = basenumber.split("");
    for(int i = 0; i < temp.length; i++)
        System.out.println(temp[i]);

    //int[] numValue = new int[temp.length];
    ArrayList<Integer>numValue = new ArrayList<Integer>();

    for(int i = 0; i < temp.length; i++)
        if (temp[i].equals('0')) 
            numValue.add(0);
        else if (temp[i].equals('1')) 
            numValue.add(1);
                     ........
        else if (temp[i].equals('a') || temp[i].equals('A'))
            numValue.add(10);
                     .........
             for(int i = 0; i < numValue.size(); i++)
        System.out.print(numValue.get(i));

基本上,我尝试将 0-9 设置为实际数字,然后从输入字符串(例如 Z3A7)中将 a-z 设置为 10-35,理想情况下将打印为 35 3 10 7

【问题讨论】:

  • 那么问题是什么?您想优化代码还是遇到一些问题?
  • 问题是我发布的代码接受该字符串并将其分解为一个数组,但仅此而已。它不会将值添加到新数组或更改值。

标签: java arrays string integer int


【解决方案1】:

在你的循环中试试这个:

Integer.parseInt(letter, 36);

这会将letter 解释为base36 数字(0-9 + 26 个字母)。

Integer.parseInt("2", 36); // 2
Integer.parseInt("B", 36); // 11
Integer.parseInt("z", 36); // 35

【讨论】:

  • 这里可能是最简单、最简洁的答案。
  • 这会导致 Integer.parseInt("");对于需要的每一行?如果是这样,那很好,我只是好奇并检查所有可能的答案。
  • @madhatter,您需要将每个字母传递给Integer.parseInt
【解决方案2】:

您可以在循环中使用这一行(假设用户没有输入空字符串):

int x = Character.isDigit(temp[i].charAt(0)) ?
        Integer.parseInt(temp[i]) : ((int) temp[i].toLowerCase().charAt(0)-87) ;

numValue.add( x );

上面代码的解释:

  • temp[i].toLowerCase() => z 和 Z 将转换为相同的值。
  • (int) temp[i].toLowerCase().charAt(0) => ASCII 字符代码。
  • -87 => 为您的规范减去 87。

【讨论】:

  • @Jeff Ascii for "Z" 是 122,所以我们应该减去 87 得到 35,op 指定。
  • 是的,我刚刚意识到这一点,并在您发布您的评论时更改了我的评论。我的坏...
  • 我正在尝试这个,一个朋友讨论了 ascii,但我们都不知道该怎么做。在我实现代码之后,它抛出了一个异常 java.lang.StringIndexOutOfBoundersException: String index out of range: 0 @ java.lang.StringcharAt(Unknown Source) @ base.mainjava:38 这就是你代码的第一行有什么东西吗我需要导入吗?
  • @madhatter 我在上面的帖子中说过 "假设用户没有输入空字符串" 并且您的字符串似乎是空的,即 temp[i ] = ""; 用户应该只输入数字(0-9)或字符(az 和 AZ),在这种情况下我的代码可以完美运行。
  • 当我运行代码时,我输入了字符串 asdf,这就是返回的内容,这可能是字符串数组的保存和拆分方式吗?
【解决方案3】:

考虑到你想将Z表示为35,我写了以下函数

更新:

Z 的 ASCII 值为 90,因此如果要将 Z 表示为 35,则应从 55 中减去每个字符,即 (90-35=55):

public static int[] convertStringArraytoIntArray(String[] sarray) throws Exception {
    if (sarray != null) {
        int intarray[] = new int[sarray.length];
        for (int i = 0; i < sarray.length; i++) {
            if (sarray[i].matches("[a-zA-Z]")) {
                intarray[i] = (int) sarray[i].toUpperCase().charAt(0) - 55;
            } else {
                intarray[i] = Integer.parseInt(sarray[i]);
            }
        }
        return intarray;
    }
    return null;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 2021-12-21
    • 2016-08-14
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    相关资源
    最近更新 更多