java - 尝试通过读取文件打印二维数组时,我的输出收到 Null
问题描述
我试图从“联系人”中读取一个文件并将其放入一个名为 sData 的二维数组中。我试图在控制台窗口上打印内容,但是我在输出中收到 null。我是否试图以不正确的方式或某些方式打印内容?代码已交付给我们,但它似乎对我没有任何帮助?
Joe - 1111245678
Tom - 3431234567
Lom - 7771234568
King - 76681234567
Dom - 6842234567
import java.io.*;
import java.util.Scanner;
public class phonenumbers {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
File f= new File ("Contacts.txt");
ReadData(f);
}
public static void printArr (String [][] hex) throws Exception{
for (int row = 0; row < hex.length; row++) {
for (int column = 0; column < hex[row].length; column++) {
System.out.print(hex[row][column] + " ");
}
System.out.println();
}
}
public static int CountLines(File f) throws Exception {
int lines = 0;
Scanner reader = new Scanner (f);
while (reader.hasNextLine()) {
String s = reader.nextLine();
s = s.trim();
if (s.length() == 0) {
break;
}
lines++;
}
reader.close();
return lines;
}
public static String [][] ReadData(File f) throws Exception {
Scanner reader = new Scanner (f);
int numLines = CountLines(f);
String [][] sData = new String [numLines] [];
for (int line = 0; line <numLines; line++ ) {
String l = reader.nextLine();
l = l.trim();
String [] temp = l.split(" ");
sData [line] = new String [temp.length];
}
printArr(sData);
reader.close();
return sData;
}
}
Output
null null null
null null null
null null null
null null null
null null null
解决方案
代替
sData [line] = new String [temp.length];
和
sData [line] = temp;
您目前只是放置一个与预期数组大小相同的空数组。
推荐阅读
- asp.net - 在 mvc 中执行操作之前的所有操作中的默认参数
- testing - 如何使用开源 testcafe 编写参数化测试?
- amazon-athena - 在 Amazon Athena 中按顺序显示分区
- time-series - 使用(多变量)奇异谱分析降维
- notepad++ - Notepad ++中的任何文件树浏览器插件是否有“始终将树同步到当前文档”选项?
- mysql - mysql 在同一张表的更新中出现语法错误
- google-chrome - 如何在 Chrome 中重新启用 html5 音量控制?
- html - 从数组值更改 CSS
- mysql - 查询:获取 3 条价值较高的记录
- c# - NullReferenceException 到 DateTime(用户设置)