简体   繁体   English

使用Java从文本文件错误中解析整数

[英]Parse Integer From Text File Error With Java

I am trying to write a function that is able to take a text file and separate each line into individual strings and adds them to an array, after which I will take the strings and convert the numbers into Integers or Doubles. 我正在尝试编写一个函数,该函数能够获取文本文件并将每一行分成单独的字符串,并将它们添加到数组中,之后我将获取字符串并将数字转换为Integers或Doubles。 However, it keeps returning a NumberFormatException whenever I try to do Integer.parseInt() on the first string in the array, which is always an integer. 但是,每当我尝试对数组中的第一个字符串进行Integer.parseInt()时,它始终返回NumberFormatException,该字符串始终是整数。

This code is a simplified version of what I am attempting to do: 这段代码是我正在尝试做的简化版本:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class test {

    public static void main(String[] args) {

        try {

            File file = new File("preprocessed_data.txt");

            Scanner scanner = new Scanner(file);

            while(scanner.hasNextLine()) {

                String line = scanner.nextLine();

                if (line.length() != 0) {

                    // Splitting each line into an array of Strings
                    String[] strings = line.split("\\s+");

                    // Trying to convert the first String into an Integer
                    System.out.println(Integer.parseInt(strings[0]));

                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

}

This is the text file which I am trying to process: 这是我要处理的文本文件:

11259 8111 +2 14 5.9 5.1 2.0 662.8449 1324.6825 1324.6817 0.0008 1 √ CDFEK($1) KLTK($1) [A1:5 P215:218] 11259 8111 +2 14 5.9 5.1 2.0 662.8449 1324.6825 1324.6817 0.0008 1√CDFEK($ 1)KLTK($ 1)[A1:5 P215:218]

10365 4551 +2 28 11.0 9.0 1.7 643.3196 1285.6320 1285.6245 0.0075 1 √ CDFEK($1) K($1)FR [A1:5 P311:313] 10365 4551 +2 28 11.0 9.0 1.7 643.3196 1285.6320 1285.6245 0.0075 1√CDFEK($ 1)K($ 1)FR [A1:5 P311:313]

16242 4175 +3 23 13.4 7.3 1.6 546.6142 1637.8280 1637.8316 -0.0035 3 √ CDFEK($1)KK($1)GDKAR [A1:6 O448:453] 16242 4175 +3 23 13.4 7.3 1.6 546.6142 1637.8280 1637.8316 -0.0035 3√CDFEK($ 1)KK($ 1)GDKAR [A1:6 O448:453]

27030 24226 +3 16 5.4 6.4 1.7 893.4433 2678.3153 2678.3178 -0.0024 2 √ KSFCAWLNVPNGNK($1) IK($1)DNNMR + OxiM(22) 27031 25071 +3 10 4.8 5.1 2.6 893.4530 2678.3445 2678.3178 0.0267 2 √ KSFCAWLNVPNGNK($1) IK($1)DNNMR + OxiM(22) [A6:19 D503:509] 27030 24226 +3 16 5.4 6.4 1.7 893.4433 2678.3153 2678.3178 -0.0024 2√KSFCAWLNVPNGNK($ 1)IK($ 1)DNNMR + OxiM(22)27031 25071 +3 10 4.8 5.1 2.6 893.4530 2678.3445 2678.3178 0.0267 2√KSFCAWLNVPNGNK($ 1) )DNNMR + OxiM(22)[A6:19 D503:509]

25104 18270 +3 19 6.8 5.8 1.7 805.7773 2415.3173 2415.2965 0.0207 2 √ KSFCAWLNVPNGNK($1) LRNLK($1) [A6:19 I271:275 A6:19 I329:333 A6:19 I369:373] 25104 18270 +3 19 6.8 5.8 1.7 805.7773 2415.3173 2415.2965 0.0207 2√KSFCAWLNVPNGNK($ 1)LRNLK($ 1)[A6:19 I271:275 A6:19 I329:333 A6:19 I369:373]

27761 30048 +3 37 6.0 6.5 1.7 959.4729 2876.4041 2876.3883 0.0158 1 √ KSFCAWLNVPNGNK($1) ELNEQAGESK($1) [A6:19 I469:478] 27761 30048 +3 37 6.0 6.5 1.7 959.4729 2876.4041 2876.3883 0.0158 1√KSFCAWLNVPNGNK($ 1)ELNEQAGESK($ 1)[A6:19 I469:478]

26769 27493 +3 17 13.0 6.4 1.3 883.4568 2648.3560 2648.3541 0.0019 1 √ KSFCAWLNVPNGNK($1) KPLDFEK($1) 26781 28982 +3 15 9.4 6.6 1.6 883.4586 2648.3611 2648.3541 0.0070 1 √ KSFCAWLNVPNGNK($1) KPLDFEK($1) [A6:19 K1379:1385] 26769 27493 +3 17 13.0 6.4 1.3 883.4568 2648.3560 2648.3541 0.0019 1√KSFCAWLNVPNGNK($ 1)KPLDFEK($ 1)26781 28982 +3 15 9.4 6.6 1.6 883.4586 2648.3611 2648.3541 0.0070 1√KSFCAWLNVPNGNK($ 1)KPLDFEK($ 1)[1)[ 1385]

And this is the error which I keep getting: 这是我不断得到的错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "11259" 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 com.company.test.main(test.java:25) 线程“主”中的异常java.lang.NumberFormatException:对于输入字符串:java.lang.Integer.parseInt(Integer.java:580)处的java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)处的“ 11259”在com.company.test.main(test.java:25)的java.lang.Integer.parseInt(Integer.java:615)

This looks like invisible characters, because your code works for me. 这看起来像看不见的字符,因为您的代码对我有用。

I would highly recommend opening the file in an editor like the notepad.exe on windows and make sure, that there are no invisible characters. 我强烈建议在Windows上的notepad.exe这样的编辑器中打开文件,并确保没有不可见的字符。

我尝试处理的文本文件是使用UTF-8编码的,一旦我将其切换为ANSI,它就会删除文件开头的不可见字符,并且代码可以正常工作。

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

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