[英]Read file then store in array without using arraylist java
I am trying to read a file with 8 columns of data and store each string of data into an array, but I keep getting cannot find symbol error for employee.length. 我正在尝试读取具有8列数据的文件并将每个数据字符串存储到一个数组中,但是我一直无法找到employee.length的符号错误。 I put 8 elements for each string within the row. 我在行中为每个字符串放置了8个元素。 Please help me by explaining what I am doing wrong. 请通过解释我做错了什么来帮助我。 Code: 码:
Scanner scan = new Scanner(new FileReader ("payrollData.dat")); 扫描仪扫描=新扫描仪(新FileReader(“ payrollData.dat”));
String employee[] = new String[8];
while(scan.hasNext())
{
for(int i = 0; i < employee.length(); i++)
{
employee[i] = scan.next();
}
}
System.out.println(employee);8
Its employee.length
and not employee.length()
. 它的employee.length
而不是employee.length()
。 In case of arrays, length is a final variable, not a method. 对于数组,length是最终变量,而不是方法。
And use Arrays.toString(employee)
to print employees properly. 并使用Arrays.toString(employee)
正确打印员工。 System.out.println(employee)
will just print the reference address. System.out.println(employee)
只会打印参考地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.