java - 将带有字母的字符串数组转换为java中的int数组

标签 java arrays string integer int

好的,我将尝试在这里解释我的问题,我需要做的是将字符串数组转换为 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

最佳答案

在你的循环中试试这个:

Integer.parseInt(letter, 36);

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

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

关于java - 将带有字母的字符串数组转换为java中的int数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13718180/

相关文章:

c - 获取用户输入并将其存储在 C 中的字符串数组中

string - 查找包含另一个字符串的某些变位词作为子序列的字符串的子字符串数

javascript - 将 javascript 字符串中的所有字母和超过 2 个空格替换为空字符

java - 如何从 webclient 中查找 HtmlUnit 中的 CurrentPage

java - BukkitRunnable 中的 'this'

java - 发送电子邮件时更改文本(字符串)的颜色

c - 如何(从函数)返回包含数组且该数组中包含正确元素的结构?

java - 定义局部变量时哪个顺序更好?

javascript - 如何在javascript中过滤和添加剩余的obj

c++ - 使用 stringstream 代替字符串? - C++