【发布时间】:2014-09-17 19:45:23
【问题描述】:
我对 Java 还很陌生,但我正在努力学习。作为一个个人实验,我正在编写一个计算电影开发过程的程序,与我经常使用的 Android 和 iPhone 的“大规模开发图表”非常相似。
按照https://www.youtube.com/watch?v=QeaXXpxNtq0 的代码,我有以下代码来读取 CSV 文件并生成二维数组:
static String xStrPath;
static String[][] myArray;
static void setUpMyCSVArray() {
myArray = new String[8702][8];
Scanner scanLn = null;
int Rowc = 0;
int Row = 0;
int Colc = 0;
int Col = 0;
String InputLine = "";
double xnum = 0;
String xfileLocation;
xfileLocation = "database.csv";
System.out.println("\n****** Setup Array ******");
try {
scanLn = new Scanner(new BufferedReader(new FileReader(xfileLocation)));
while (scanLn.hasNextLine()) {
InputLine = scanLn.nextLine();
String[] InArray = InputLine.split(",");
for (int x = 0; x < InArray.length; x++) {
myArray [Rowc] [x] = InArray[x];
}
Rowc++;
}
} catch (Exception e) {
System.out.println(e);
}
System.out.println(Arrays.toString(myArray[0]));
}
CSV 文件如下所示:
Film Name Developer Dilution ISO/ASA 35mm 120 Sheet Temperature
Adox CHM 125, 510-Pyro, 1+100, 125, 7, , , 21C
Adox CHM 125, 510-Pyro, 1+500, 125, 50, , , 21C
Adox CHM 125, 777, stock, 100, 12, , , 24C
Adox CHM 125, A03, stock, 125, 6.5, , , 20C
Adox CHM 125, ACU-1, 1+10, 100, 9, , , 21C
Adox CHM 125, Acufine, stock, 125, 4, , , 20C
Adox CHM 125, Acufine, stock, 200, 6, , , 20C
Adox CHM 125, Acufine, stock, 320, 5, , , 21C
当我跑步时:
System.out.println(Arrays.toString(myArray[0]));
输出是:
Adox CHM 125, 510-Pyro, 1+100, 125, 7, , , 21C
但我无法分离该行的数据,即我想运行
System.out.println(Arrays.toString(myArray[0][0]));
输出:
Adox CHM 125
经过多次尝试,我对我的代码出了什么问题感到有点困惑,非常感谢任何帮助或建议。
【问题讨论】:
-
我认为您没有正确读取拆分后的字符串。看看这个:stackoverflow.com/questions/5360628/…
-
运行
System.out.println(Arrays.toString(myArray[0][0]));时会打印什么?无论如何,我认为您可以省略Arrays.toString部分。只需打印myArray[0][0]
标签: java arrays netbeans multidimensional-array