简体   繁体   中英

Inserting a text file into 2d array

I want to read a text file and insert it into 2d double array. I have this code, but the first problem is that it doest not read the last column and the second problem is that it returns just the first line of my text file. For example, if the text file is

1.1,2.1,3.1
2.1,1.1,4.1
1.3,3.2,5.1

It returns:

{1.1 2.1}

How should I fix it?

BufferedReader match_dataset = new BufferedReader(new FileReader("test.txt"));
Scanner src = new Scanner(match_dataset);
ArrayList<Double> lines = new ArrayList<Double>();
src.useDelimiter(",");

while (src.hasNextDouble()) {
    lines.add(src.nextDouble());
}
Double[] temp_match = new Double[lines.size()];
lines.toArray(temp_match);

You have the wrong syntax for a double array. Should be two brackets [][] after double

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