从txt文件读取数据的问题

时间:2018-08-19 01:47:39

标签: 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)

sFilefile:

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

我还有其他一些sleg数组和其他整数范围,但没有任何问题。非常感谢您的建议。

1 个答案:

答案 0 :(得分:1)

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

split(" ");

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

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