Javascript 如何从txt文件中读取双值

Javascript 如何从txt文件中读取双值,javascript,java,arrays,double,Javascript,Java,Arrays,Double,我有一个带有一些变量的类“DataReader”,我想把一个文件中的值放入其中 对于预算,txt文件的第一行,;二线工程数量;而第三条线的部门数量也没有问题 问题是当我试图填充双倍数组时 我发现的另一个问题是设置数组的长度。我在这个例子中使用了4000行,因为我的文件有4003行,前3行是预算、项目和部门。但实际上数组应该和文件长度一样长,而不一定是4000 public DataReader() { this.budget = 0; th

我有一个带有一些变量的类“DataReader”,我想把一个文件中的值放入其中

对于预算,txt文件的第一行,;二线工程数量;而第三条线的部门数量也没有问题

问题是当我试图填充双倍数组时

我发现的另一个问题是设置数组的长度。我在这个例子中使用了4000行,因为我的文件有4003行,前3行是预算、项目和部门。但实际上数组应该和文件长度一样长,而不一定是4000

    public DataReader() {
            this.budget = 0;
            this.numberOfDepartments = 0;
            this.numberOfProjects = 0;
            this.array = new double[4000][this.numberOfProjects];
    }

    public void readDataFromFile() throws FileNotFoundException {
            String string = null;
            FileReader file = new FileReader("C:/Users/Andy/Desktop/myfile.txt");    //open a communication buffer with the txt file
            Scanner reader = new Scanner(file);    //Initialize a scanner to read the txt file

            /* READ THE BUDGET */
            string = reader.nextLine();   //read the first line
            this.budget = Integer.parseInt(removeOneCommaFromString(string)); //remove the comma and transform it in an integer
            System.out.println(this.budget);        //test

            /* READ THE NUMBER OF DEPARTMENTS */
            string = reader.nextLine();
            this.numberOfDepartments = Integer.parseInt(removeOneCommaFromString(string));
            System.out.println(this.numberOfDepartments);

            /* READ THE NUMBER OF PROJECTS */
            string = reader.nextLine();
            this.numberOfProjects = Integer.parseInt(removeOneCommaFromString(string));
            System.out.println(this.numberOfProjects);

            /* READ THE MATRIX */
            for(int i = 0; i < 4000; i++) {
                    string = reader.nextLine();
                    string = removeCommasFromString(string);
                    char[] temp = string.toCharArray();
                    char[] temp2 = new char[temp.length];
                    for(int z = 0; z < this.numberOfProjects; z++) {
                            for(int j = 0; j < temp.length; j ++) {
                                    if(temp[j] >= 0 || temp[j] <= 9 || temp[j] == '.') {
                                            temp2[j] = temp[j];
                                    }
                                    else {
                                            String newString = new String(temp2);
                                            Double number = new Double(newString);
                                            array[i][z] = number;
                                            System.out.println(array[i][z]);
                                    }
                            }
                    }
            }

    }

    private String removeCommasFromString(String string) {
            char[] stringWithoutCommas = new char[string.length()];
            char[] stringWithCommas = string.toCharArray();

            for(int i = 0; i < stringWithCommas.length; i++) {
                    if(stringWithCommas[i] != ',') {
                            stringWithoutCommas[i] = stringWithCommas[i];
                    }
                    else {
                            stringWithoutCommas[i] = ' ';
                    }
            }
            String correctString = new String(stringWithoutCommas);
            return correctString;
    }

    private String removeOneCommaFromString(String string) {
            // TODO Auto-generated method stub
            char[] array1 = string.toCharArray();
            char[] array2 = new char[array1.length -1];

            for(int i = 0; i < array1.length-1; i++) {
                    array2[i] = array1[i];
            }

            String stringWithoutComma = new String(array2);
            return stringWithoutComma;
    }

    public static void main(String[] args) throws FileNotFoundException {
            DataReader ld = new DataReader();
            ld.readDataFromFile();
    }
}

我已经把文件的格式清理了一点,但是你能确切地解释一下你遇到的问题是什么吗?运行代码时会发生什么?你有错误吗?如果是,请将错误消息编辑到问题中。你得到了错误的数据吗?一些正确的数据?什么都没有?谢谢andrewsi,问题是即使数组以正确的方式编译和运行,程序也不会打印任何数量的数组。。它只打印:3420 2 10。所以它要么不在数组中注册数据,要么就是不打印。
3420,
2,
10,
103.8421,1250.434,54.56157,36.92909,962.7694,173.022,106.7644,404.1759,11.66381,2966.675,
0.1270486,0.1229978,0.06825768,0.2493465,0.1555537,0.05003887,0.2638902,0.1703413,0.2503172,0.1844837,
104.5753,446.7093,825.9758,1220.173,726.259,508.811,362.4495,758.9695,258.2516,1955.544,
0.05048114,0.07712628,0.1827393,0.06109419,0.1818522,0.1821966,0.1377791,0.07180541,0.07752416,0.06139474,
444.7019,461.5938,976.8734,273.714,184.7028,229.7422,130.7461,84.34789,49.8125,417.2332,
0.1141632,0.06448551,0.08264564,0.08626967,0.2759337,0.2762382,0.1682017,0.2879532,0.1430924,0.2055033,
53.11755,479.1441,49.23315,188.405,1127.118,117.5416,105.4314,78.87017,50.0128,719.333,