Java 尝试将3个字符的字符串转换为整数时出现NumberFormatException

Java 尝试将3个字符的字符串转换为整数时出现NumberFormatException,java,numbers,integer,numberformatexception,Java,Numbers,Integer,Numberformatexception,线程“main”java.lang.NumberFormatException中的异常:对于输入字符串:“l20” 这是我在尝试将一个3位数的字符串键入int时收到的错误消息 如果我理解正确,java中int的最大值是2147483647 这是导致语法错误的方法 private int getRed(String key) { return Integer.parseInt(key.substring(3,6)); } 编辑:为了澄清,键是由以下代码随机生成的12位字符串 for(in

线程“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));
}

for(int i=0;i使用调试器或在方法内部放置一个
System.out.println
getRed(String key)
来检查
key.substring(3,6)
的值。得到的异常是因为
key.substring(3,6)
没有返回数字

private int getRed(String key) {
    System.out.println(key.substring(3,6));
    return Integer.parseInt(key.substring(3,6));
}
正如我看到的,您尝试从“l20”逐整数.parseInt(key.substring(3,6))获取整数;
我知道parseInt()解析整数(数字)。什么数字是“l”?)我不知道,整数也不知道,这就是它引发此异常的原因)

也许可以将您的代码与我的代码进行比较。我基本上是按照你说的方式做的,只有一点点不同

$ 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);
    }

}
$javac Num.java&&java Num
完整密钥:255142125179
解析:142
解析:142
$cat Num.java
导入java.util.Random;
公共类数{
公共静态int getRed(字符串键){
System.out.printf(“解析:%s\n”,key.substring(3,6));
返回Integer.parseInt(key.substring(3,6));
}
公共静态void main(字符串[]args){
字符串键=新字符串(“”);
随机=新随机();
对于(int-index=0;index<12;++index){
key=key+Integer.toString(random.nextInt(10));
}
System.out.printf(“完整密钥:%s\n”,密钥);
int value=getRed(键);
System.out.printf(“已解析:%d\n”,值);
}
}

我看没问题。它似乎工作正常,所以您没有包括一些重要的内容。

我刚刚发现了问题所在 我没有将字符串键初始化为“”,因此它默认为null 它试图转换的子串3-6以L开头

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


谢谢所有帮助过你的人

第一个数字是“一”吗?在我看来,它几乎像一个大写的“眼睛”。vi说它是小写的“呃”。我很困惑,字母中不应该有Lstring@Icarus我认为您需要编辑您的问题,以提供更多关于此错误是如何发生的上下文。我同意你的观点,你迄今为止给我们展示的不会导致你所犯的错误。请提供一个提示:
Integer.toString()在字符串连接中不需要。
。。。
$ 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);
    }

}