简体   繁体   中英

Issue with reading data from a txt file

I am reading most of the data from text files. This time, I have happened to have some issues with reading the data array from a .txt file. I am providing some part of the code as follows: 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(
            "-----------------------------------------------------------------------------------------------------------------------------------------------");


    }
}

And as I run the code, an error is popping up as follows:

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)

The 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

And the zat file:

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

I had some other arrays of slegs and other integer ranges and I did not have any issue. I would appreciate your recommendations.

I do not see four spaces in your data files, but the error message is 'For input string: "0 0"' I guess you have a tabulation in your file. You do not see it in your editor, but it might was converted to four spaces. Try to replace your

split(" ");

to

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

"\\s+" will understand several spaces and the tabulations as well. I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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