【问题标题】:NumberFormatException when I try to convert a 3 character string into integer当我尝试将 3 个字符串转换为整数时出现 NumberFormatException
【发布时间】:2020-02-12 13:41:23
【问题描述】:

“线程“main”中的异常java.lang.NumberFormatException:对于输入字符串:“l20””

这是我收到的尝试将 3 位字符串类型转换为 int 的错误消息。

如果我理解正确的话,java 中 int 的最大值是 2147483647?

这是导致语法的方法

private int getRed(String key) {
    return Integer.parseInt(key.substring(3,6));
}

编辑:为澄清起见,密钥是由以下代码随机生成的 12 位字符串

for(int i=0;i<12;i++) {
      Random random = new Random();
      key=key+Integer.toString(random.nextInt(10));
}

Edit 2:below 是一个最小的可重现示例,它会产生此错误消息 “线程“主”java.lang.NumberFormatException 中的异常:对于输入字符串:“l35” 在 java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在 java.lang.Integer.parseInt(Integer.java:580) 在 java.lang.Integer.parseInt(Integer.java:615) 在 Test.getRed(Test.java:57) 在 Test.getKey(Test.java:44) 在 Test.encode(Test.java:36) 在 Test.main(Test.java:70)"

public class Test {
    String key; 
    public Test() {
        for(int i=0;i<12;i++) {
            Random random = new Random();
            key=key+Integer.toString(random.nextInt(10));
        }
    }

    public void encode() {
        for(int i=0; i<5;i++) {
            int key=getKey(i);
        }
    }
    private int getKey(int i) {
        int indicator = i%3;
        int returnInt=0;
        switch (indicator) {
        case 0:
            returnInt=getRed(key);
            break;
        case 1:
            returnInt=getGreen(key);
            break;
        case 2:
            returnInt=getBlue(key);
            break;
        }
        return returnInt;
    }

    private int getRed(String key) {
        return Integer.parseInt(key.substring(3,6));
    }

    private int getGreen(String key) {
        return Integer.parseInt(key.substring(6,9));
    }

    private int getBlue(String key) {
        return Integer.parseInt(key.substring(9,11));
    }

    public static void main(String args[]) {    
        Test test=new Test();
        test.encode();
    }
}


【问题讨论】:

  • 第一个数字是“一”吗?在我看来,它几乎就像是一只大写的“眼睛”。
  • vi 表示它是小写的“ell”。
  • 我很困惑,字符串中不应该有 L
  • @Icarus 我认为您需要编辑您的问题以提供有关此错误如何发生的更多背景信息。我同意您的观点,到目前为止您向我们展示的内容不会导致您遇到的错误。你能提供一个minimal reproducible example吗?
  • 提示:字符串连接中不需要Integer.toString()...

标签: java numbers integer numberformatexception


【解决方案1】:

也许将您的代码与我的代码进行比较。我几乎按照你说的做,只有细微的差别。

$ javac Num.java && java Num
Full key: 255142125179
Parsing: 142
Parsed: 142
$ cat Num.java
import java.util.Random;

public class Num {
    public static int getRed(String key) {
        System.out.printf("Parsing: %s\n", key.substring(3, 6));
        return Integer.parseInt(key.substring(3,6));
    }

    public static void main(String[] args) {
        String key = new String("");
        Random random = new Random();

        for (int index = 0; index < 12; ++index) {
            key = key + Integer.toString(random.nextInt(10));
        }

        System.out.printf("Full key: %s\n", key);
        int value = getRed(key);
        System.out.printf("Parsed: %d\n", value);
    }

}

我认为没有问题。它似乎工作正常,所以你没有包含重要的东西。

【讨论】:

    【解决方案2】:

    正如我所见,您尝试从“l20”通过 Integer.parseInt(key.substring(3,6)); 获取整数; 据我所知 parseInt() 解析整数(数字)。 “l”是什么数字?)我不知道,整数不知道,这就是它抛出这个异常的原因)

    【讨论】:

      【解决方案3】:

      我刚刚发现出了什么问题 我没有将字符串键初始化为“”所以它默认为空 它尝试转换的子字符串 3-6 以 L

      开头

      一旦我修复它,错误就消失了

      感谢所有帮助过的人

      【讨论】:

        猜你喜欢
        • 2012-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-29
        • 1970-01-01
        • 2013-02-28
        • 1970-01-01
        相关资源
        最近更新 更多