简体   繁体   中英

How do I store doubles read from a file into an array?

What I am attempting to do here is to use three different arrays to store each dimension of a box by getting the data from a text file, what I want to do is to read the first line of the file, store it into a string array and then convert that data into double to store it into a double array. I have read to use ArrayLists but I want to do it with regular arrays first. I would appreciate any recommendations on this as well as any tips you have for me to learn java better as I am relatively new to programming.

public static void main(String[] args) throws IOException{
    FileReader fr = new FileReader("info.txt");
    BufferedReader br = new BufferedReader(fr);
    String nums;
    int count =0;
    String[] numbers;
    double[] length;
    double[] width;
    double[] height;

    while((nums = br.readLine()) != null){
        for(int t = 0; t < nums.length; t++){
            numbers[t] = nums.split("");
        }
    }       
}
int lineNo = 0;

while((nums = br.readLine()) != null){

        String numbers[] = nums.split(" "); // hoping you have space sepearated 3 double values in a line
        length[lineNo] = Double.parseDouble(numbers[0]);
        width[lineNo] = Double.parseDouble(numbers[1]);
        height[lineNo] = Double.parseDouble(numbers[2]);
        lineNo++;

}

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