gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:07:46 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com