简体   繁体   English

为什么无法使用java中的方法Integer.valueOf()解析从文本中读取的实际数字字符串?

[英]Why an actual number string read from a text can't be parsed with method Integer.valueOf() in java?

Why an actual number string read from a text can't be parsed with method Integer.valueOf() in java? 为什么无法使用java中的方法Integer.valueOf()解析从文本中读取的实际数字字符串?

Exception : 例外

Exception in thread "main" java.lang.NumberFormatException: For input string: "11127"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.valueOf(Integer.java:766)
    at sharingBike.ReadTxt.readRecord(ReadTxt.java:91)
    at sharingBike.ReadTxt.main(ReadTxt.java:17)

This is my code 这是我的代码

        File fileView = new File(filePath);

        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileView), "UTF-8"));

        String line;

        int count = 0;
        while ((line = in.readLine()) != null) {
            String[] lins = line.split(";");

            int value;
            value = Integer.valueOf(lins[0]);}

Here is the content of your string: 这是你的字符串的内容:

System.out.println(Arrays.toString("11127".getBytes()));

which outputs: 哪个输出:

[-17, -69, -65, 49, 49, 49, 50, 55] [-17,-69,-65,49,49,49,50,55]

The first three bytes are a UTF-8 BOM . 前三个字节是UTF-8 BOM

You can fix it by removing non-digits from the string first (and use parseInt to return an int instead of an Integer ): 您可以通过首先从字符串中删除非数字来修复它(并使用parseInt返回int而不是Integer ):

int value = Integer.parseInt(lins[0].replaceAll("\\D", "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在Integer类的Integer.valueOf方法中使用assert? - Why is assert used in the Integer.valueOf method of the Integer class? 为什么 Integer.valueOf 无法将十六进制字符串解析回 integer - Why Integer.valueOf cannot parse the hex string back to integer Java Integer.valueOf为范围内的有效数字生成NumberFormatException - Java Integer.valueOf produces NumberFormatException for valid number within range C#中的Java Integer.ValueOf方法等价与Radix参数 - Java Integer.ValueOf method equivalence in C# With Radix Parameter Integer.valueOf 不适用于 Java 中 -1 的二进制表示 - Integer.valueOf not working for binary representation of -1 in Java 为什么Long.valueOf(0).equals(Integer.valueOf(0))为false? - Why is Long.valueOf(0).equals(Integer.valueOf(0)) false? Integer.valueOf()的值 - The value of Integer.valueOf() 为什么 == 与 Integer.valueOf(String) 的比较给出 127 和 128 的不同结果? - Why do == comparisons with Integer.valueOf(String) give different results for 127 and 128? 为什么数字字符的 Integer.valueOf 不返回其值? - Why doesn't Integer.valueOf of a digit Character return its value? 使用Integer.valueOf(String)查找“使用解析基元进行装箱/取消装箱”的Findbugs - Findbugs issue with “Boxing/unboxing to parse a primitive” with Integer.valueOf(String)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM