java - 从txt文件读取数据的问题

标签 java numberformatexception reader

我正在从文本文件中读取大部分数据。这次,我在从 .txt 文件读取数据数组时遇到了一些问题。我提供部分代码如下: MWE:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Model {
    public static final int Y = 7;
    public static final int K = 42;
    public static final int T = 10;

public static class Sleg {
        // private int id;
        private int i;
        private int j;
        private double l;

        public Sleg(int i, int j, double l) {
            // this.id = id;
            this.i = i;
            this.j = j;
            this.l = l;
        }

        @Override
        public String toString() {
            return Integer.toString(i) + " " + Integer.toString(j) + " " + Double.toString(l);
        }
    }


    public static void dataGen() {

 List<Sleg> slegs = new ArrayList<Sleg>();
    File slFile = new File("slFile.txt");

    try (BufferedReader reader = new BufferedReader(new FileReader("slFile.txt"))) {


        String line;
        while ((line = reader.readLine()) != null) {

            String[] numbers = line.trim().split(" ");
            int i = Integer.parseInt(numbers[0]);
            int j = Integer.parseInt(numbers[1]);
            double l = Double.parseDouble(numbers[2]);
            slegs.add(new Sleg(i, j, l));
        }

    } catch (FileNotFoundException e) {
        System.out.println(slFile.toString() + " does not exist.");
    } catch (IOException e) {
        // Handle any possible IOExceptions as well...
        System.out.println("Unable to read : " + slFile.toString());
    }

    System.out.print("slegs = [");
    for (Sleg s : slegs) {
        System.out.print("[" + s + "] ");
    }
    System.out.println("]");
    System.out.println("-----------------------------------------------------------------------------------------------------------------------------------------------");
Zat[][] u = new int [slegs.size()][T];
File zatFile = new File("zat.txt");
    try (BufferedReader br = new BufferedReader(new FileReader("zat.txt"))) {
        String line;
        while ((line = br.readLine()) != null) {
            for (int sl = 0; sl < slegs.size(); sl++) {
                String[] ln = line.trim().split(" ");
                for (int t = 0; t < T; t++) {
                    Zat[sl][t] = Integer.parseInt(ln[t]);
                }
            }
        }
    } catch (FileNotFoundException e) {
        System.out.println("Unable to find : " + zatFile.toString());
    } catch (IOException e) {
        System.out.println("Unable to read : " + zatFile.toString());
    }catch (NumberFormatException e) {
           System.out.println("not an integer"); 
       }
    System.out.print("Zat = [");
    for (int sl = 0; sl < slegs.size(); sl++) {
        System.out.print("[");
        for (int t = 0; t < T; t++) {
            System.out.print(" " + Zat[sl][t] + " ");
        }
        System.out.print("]");
    }
    System.out.println("]");
    System.out.println(
            "-----------------------------------------------------------------------------------------------------------------------------------------------");


    }
}

当我运行代码时,会弹出一个错误,如下所示:

Exception in thread "main" java.lang.NumberFormatException: For input string: "0    0"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at Model.dataGen(Model.java:832)
    at SolverMethod.main(SolverMethod.java:9)

sFile文件:

1 2 400
2 5 800
5 7 450
2 3 800
3 6 550
3 4 500
4 5 500
7 5 450
5 4 500
4 3 400
6 3 550
3 2 700
2 1 400
5 2 800

和 zat 文件:

1 0 0 0 1 0 0 0 0 0
1 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 1 0 0 1 0 0 0 0 1
0 1 0 0 0 0 0 0 0 0
0 0 1 0 1 0 0 0 1 0
0 0 1 0 0 0 0 0 1 0
0 0 0 1 0 0 1 0 0 0
0 0 0 1 0 1 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 1 0 0 1

我有一些其他的雪橇数组和其他整数范围,但没有任何问题。我将不胜感激您的建议。

最佳答案

我在您的数据文件中没有看到四个空格,但错误消息是“对于输入字符串:“0 0”” 我猜你的文件里有一个表格。您在编辑器中看不到它,但它可能已转换为四个空格。 尝试更换您的

split(" ");

split(""\\s+""); \\ Thanks for the comment, I did not think about several whitespace characters.

“\s+”也会理解几个空格和表格。 我希望这会有所帮助。

关于java - 从txt文件读取数据的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51913817/

相关文章:

java - 在 Ibatis 中防止 SQL 注入(inject)

java - 重播井字游戏

java - NumberFormatException Android Java

java - Java中用点和冒号处理不同的数字

java - 附加两个十进制数字字符串并解析为 double

c# - "There is already an open DataReader associated with this Command which must be closed first."

android - ImageReader Android 无法读取任何图像

java - 如何定义半满多维数组?

java - BufferedWriter 在刷新并关闭后不写入文件

java - openjdk 8补丁版本