为什么 Integer.parseInt 在看似有效的输入上抛出 NumberFormatException?

发布于 2024-08-20 21:45:03 字数 1299 浏览 5 评论 0原文

我正在做一个书上的简单练习,我对 java 函数 parseInt 的工作原理有点困惑。我从输入文件中读取了一行,使用 StringTokenizer 将其拆分,现在我想将每个部分解析为整数。

我已在监视窗口中检查过 parseInt 函数的输入确实是一个看起来有效整数的字符串(例如“35”)。但是,当我尝试在保存值“35”的变量 str 上使用 str.charAt 函数时,我得到了奇怪的结果:

str.charAt(0) == ""
str.charAt(1) == "3"
str.charAt(2) == ""
str.charAt(3) == "5"

这似乎是一个问题,可能在某种程度上与编码相关,所以我尝试使用这种读取文件的方式来修复它:(

InputStreamReader reader = new InputStreamReader(new FileInputStream(inputfile), "UTF-8");

我已在编辑器中使用 UTF-8 编码显式保存文件),但这没有帮助。有什么想法可能是什么问题以及如何解决它吗?

编辑:我的样本

        InputStreamReader reader = new InputStreamReader(new FileInputStream(inputfile), "UTF-8");
        BufferedReader bfreader = new BufferedReader(reader);

        line = bfreader.readLine();
        while (line !=null)
        {
                String[] valueStrings = line.split(" ");
                String hole = valueStrings[0]; 

                int[] values = new int[4];
                for (int i = 0; i <values.length; i++){

                    String nr = valueStrings[i+1].trim(); 
                    values [i] = Integer.parseInt(nr);
                }

                // it breaks at the parseInt here, the rest is not even executed...

         }

I'm doing a simple exercise from a book and I'm a little bit confused with how the java function parseInt works. I have read a line from an input file, used the StringTokenizer to split it and now I want to parse each part as an integer.

I have checked in the watch window that the input of the parseInt function is indeed a string which seems a valid integer (e.g. "35"). However, when I try to use the str.charAt function on my variable str holding the value "35", I get strange results :

str.charAt(0) == ""
str.charAt(1) == "3"
str.charAt(2) == ""
str.charAt(3) == "5"

This seems to be a problem probably somehow related to the encoding, so I have tried to fix it using this way of reading the file :

InputStreamReader reader = new InputStreamReader(new FileInputStream(inputfile), "UTF-8");

(I have explicitly saved the file using UTF-8 encoding in my editor), but this didn't help. Any ideas what could be the problem and how to fix it ?

EDIT : My sample

        InputStreamReader reader = new InputStreamReader(new FileInputStream(inputfile), "UTF-8");
        BufferedReader bfreader = new BufferedReader(reader);

        line = bfreader.readLine();
        while (line !=null)
        {
                String[] valueStrings = line.split(" ");
                String hole = valueStrings[0]; 

                int[] values = new int[4];
                for (int i = 0; i <values.length; i++){

                    String nr = valueStrings[i+1].trim(); 
                    values [i] = Integer.parseInt(nr);
                }

                // it breaks at the parseInt here, the rest is not even executed...

         }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

却一份温柔 2024-08-27 21:45:03

我的猜测是,它实际上

str.charAt(0) == '\0'
str.charAt(1) == '3'
str.charAt(2) == '\0'
str.charAt(3) == '5'

听起来它可能实际上是以 UTF-16 而不是 UTF-8 格式保存的 - 但如果您的文本编辑器认为它意味着要保存“ null”字符,这是有道理的。尝试在二进制十六进制编辑器中查看文本文件 - 我怀疑您会发现每个其他字节都是 0。

如果这没有帮助,请发布一个简短但完整的程序来演示问题 - 到目前为止我们只看到你的一行代码。

My guess is that it's actually:

str.charAt(0) == '\0'
str.charAt(1) == '3'
str.charAt(2) == '\0'
str.charAt(3) == '5'

It sounds like it's probably actually saved in UTF-16 rather than UTF-8 - but if your text editor thought it was meant to save "null" characters, that would make sense. Try looking at the text file in a binary hex editor - I suspect you'll find that every other byte is 0.

If that doesn't help, please post a short but complete program which demonstrates the problem - so far we've only seen one line of your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文