简体   繁体   English

Integer.parseInt()问题

[英]Integer.parseInt() issues

I'm having a bit of a problem trying to get my code to work. 我试图让我的代码工作时遇到一些问题。 I am working on a project for my computer science class, and I have to get my program to read the file and perform some math. 我正在为我的计算机科学课做一个项目,我必须让我的程序读取文件并执行一些数学运算。 When I tried doing this, the code was not working. 当我尝试这样做时,代码无法正常工作。 I then checked with a friend who wrote the exact same code, and it did not work. 然后我和一位编写相同代码的朋友核实了,但是没有用。

The input .txt file that the program reads looks like this: 2/3,4/5 -1/6,2/4 1/1,1/1 程序读取的输入.txt文件如下所示:2 / 3,4 / 5 -1 / 6,2 / 4 1 / 1,1 / 1

The code I have written looks like this: 我写的代码看起来像这样:

import javax.swing.JFileChooser;

import java.util.*;

public class ProjectTest
{

    public static void main(String[] args) throws Exception
    {           

        JFileChooser chooserRational = new JFileChooser();
        int returnValRational = chooserRational.showOpenDialog(null);
        if(returnValRational == JFileChooser.APPROVE_OPTION)
        {
            System.out.println("You chose to open this file: " + chooserRational.getSelectedFile().getName());

            Scanner input = new Scanner(chooserRational.getSelectedFile());

            while(input.hasNext() == true)
            {
                String line = input.nextLine();
                String[] output = line.split(",");
                String[] output1 = output[0].split("/");
                String[] output2 = output[1].split("/");

                String a = output1[0];
                String b = output1[1];
                String c = output2[0];
                String d = output2[1];

                int int1 = Integer.parseInt(a);
                int int2 = Integer.parseInt(b);
                int int3 = Integer.parseInt(c);
                int int4 = Integer.parseInt(d);

                System.out.println(int1 + " " + int2 + " " + int3 + " " + int4);


            }
            input.close();
        }
    }
}

When I output just the Strings a, b, c, and d, the code works perfectly fine and outputs the values perfectly. 当我只输出字符串a,b,c和d时,代码完美地运行并完美地输出值。 When the code sees Integer.parseInt(a) , however, it gives me an error that looks like this: 但是,当代码看到Integer.parseInt(a)时,它会给出一个如下所示的错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "?2"
  at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
  at java.lang.Integer.parseInt(Integer.java:580)
  at java.lang.Integer.parseInt(Integer.java:615)
  at ProjectTest1.main(ProjectTest1.java:33)

Any help would be greatly appreciated. 任何帮助将不胜感激。

Because your data file contains an UTF-8 BOM . 因为您的数据文件包含UTF-8 BOM

You have two alternatives: edit your source data file to remove the BOM, or you can add some code to deal with the BOM. 您有两种选择:编辑源数据文件以删除BOM,或者您可以添加一些代码来处理BOM。 For the first option use Notepad++ and remove the BOM. 对于第一个选项,请使用Notepad ++并删除BOM。 For the second alternative: 对于第二种选择:

Scanner input = new Scanner(chooserRational.getSelectedFile());

if (input.nextByte() == 0xFE) {
  input.nextByte();
  input.nextByte();
} else {
  input = new Scanner(chooserRational.getSelectedFile());
}

You should replace 你应该更换

String line = input.nextLine();

with

String line = input.next();

since you have multiples groups of data on the same line. 因为你在同一行上有多个数据组。

Edit : 编辑:

I ran your code and did not get the same exception as you. 我运行你的代码并没有得到与你相同的例外。 I had a NumberFormatException due to the nextLine call, I now fixed it and it runs with no error. 由于nextLine调用,我有一个NumberFormatException ,我现在修复它,它运行没有错误。 I think like the others that you have an encoding problem. 我认为和其他人一样,你有编码问题。 Search on the internet how to display invisible characters on your preferred text editor. 在互联网上搜索如何在首选文本编辑器上显示不可见字符。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM