加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

为什么无法使用java中的方法Integer.valueOf()解析从文本中读取

发布时间:2020-12-15 01:12:00 所属栏目:Java 来源:网络整理
导读:为什么无法使用java中的方法Integer.valueOf()解析从文本中读取的实际数字字符串? 例外: Exception in thread "main" java.lang.NumberFormatException: For input string: "11127" at java.lang.NumberFormatException.forInputString(NumberFormatExcept

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

例外:

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)

这是我的代码

        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]);}
最佳答案
这是你的字符串的内容:

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

哪个输出:

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

前三个字节是a UTF-8 BOM.

您可以通过首先从字符串中删除非数字来修复它(并使用parseInt返回int而不是Integer):

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读