[英]How to import a 2D float matrix from .txt file into a 2D float array in Java?
[英]How to print 2D Array from .txt file in Java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class csvimport5 {
public static void main(String[] args) throws IOException {
double [][] data = new double [87][2];
File file = new File("buydata.txt");
int row = 0;
int col = 0;
BufferedReader bufRdr = new BufferedReader(new FileReader(file));
String line = null;
//read each line of text file
while((line = bufRdr.readLine()) != null && row < data.length)
{
StringTokenizer st = new StringTokenizer(line,",");
while (st.hasMoreTokens())
{
//get next token and store it in the array
data[row][col] = Double.parseDouble(st.nextToken());
col++;
}
col = 0;
row++;
}
System.out.println(" "+data[87][2]);
}
}
錯誤信息:
ArrayIndexOutOfBounds Exception at "System.out.println(" "+data[87][2]);
我的txt文件是:
8.00,28.00
18.00,28.00
8.00,23.00
12.00,20.00
15.00,30.00
12.00,32.00
12.00,20.00
18.00,31.00
29.00,25.00
6.00,28.00
7.00,28.00
6.00,24.00
14.00,30.00
11.00,23.00
12.00,20.00
31.00,24.00
11.00,20.00
17.00,23.00
14.00,32.00
15.00,23.00
8.00,20.00
17.00,31.00
7.00,20.00
12.00,23.00
15.00,20.00
12.00,20.00
21.00,20.00
27.00,27.00
18.00,20.00
25.00,27.00
46.00,13.00
26.00,10.00
47.00,22.00
44.00,14.00
34.00,4.00
34.00,4.00
44.00,7.00
39.00,5.00
20.00,0.00
43.00,11.00
43.00,25.00
34.00,2.00
25.00,10.00
50.00,9.00
25.00,9.00
39.00,2.00
34.00,7.00
44.00,15.00
36.00,3.00
40.00,5.00
49.00,21.00
42.00,7.00
35.00,1.00
30.00,2.00
31.00,13.00
53.00,12.00
40.00,4.00
26.00,4.00
50.00,55.00
57.00,51.00
62.00,52.00
56.00,52.00
59.00,40.00
61.00,68.00
66.00,49.00
57.00,49.00
62.00,58.00
47.00,58.00
53.00,40.00
60.00,54.00
55.00,48.00
56.00,65.00
67.00,56.00
55.00,43.00
52.00,49.00
67.00,62.00
68.00,61.00
65.00,58.00
46.00,53.00
46.00,49.00
47.00,40.00
64.00,22.00
64.00,54.00
63.00,64.00
63.00,56.00
64.00,44.00
63.00,40.00
data[87][2]
確實是一個 OutOfBound 索引。
如果您使用[87][2]
實例化您的數組,您的最后一個索引是data[86][1]
,因為您的索引范圍為[0...86]
和[0...1]
double [][] data = new double [87][2];
將創建一個大小為 87 和 2 的 double[][]。
引用這些條目的索引將是 86 和 1(數組總是以索引“0”而不是“1”開始)
嘗試:
System.out.println(" "+data[86][1]);
那應該工作!
Java 中的任何數組 object 的索引為“n-1”。 因此,數組的邊界是 0 和 86,其中 data[0][0] 為您提供第一個元素,而 data[86][1] 為您提供最后一個元素。
代替:
System.out.println(" "+data[87][2]);
和:
System.out.println(" "+data[86][1]);
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
public class CSVReader {
public static void main(String[] args) throws IOException {
List<String[]> dataList = new ArrayList();
File file = new File("/tmp/data.txt");
List<String> lines = FileUtils.readLines(file, "UTF-8");
for (String line : lines) {
dataList.add(line.split(","));
}
String[] val = dataList.get(dataList.size() - 1);
System.out.println("" + val[0] + " " + val[1]);
}
}
正如其他人所指出的那樣,您只是弄錯了索引。 除此之外,上面的代碼是一種更清晰的處理方式,可能會稍微節省一些......注意使用 apache 和內置字符串 api。 在方便的地方將您的字符串添加到雙重轉換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.