我的数组打印的次数与其索引的打印次数相同,例如,将其打印为null
1
12
123
1234
12345
我正在尝试通过一个文本文件逐行打印该数组,该文本文件包含20行4条数据。
我尝试将print语句移到while循环之外,并且只在长行中显示该数组。
public static void main(String[] args)
{
System.out.println("Hello! Welcome to my program. ");
System.out.println("This Program will read and display a text file in an array");
String[] arr;
try
{
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
arr = new String[50];//
int i = 0;
String line;
while ((line = br.readLine()) != null)
{
arr[i++] = line;
System.out.println(i + "." + Arrays.toString(arr) + "\n");
}
br.close();
} catch (IOException e)
{
System.out.println("File not Found");
}
}
答案 0 :(得分:0)
更改此部分。
while ((line = br.readLine()) != null)
{
arr[i] = line; // was changed to arr[i] from arr[i++]
System.out.println(i + "." + arr[i++] + "\n"); // was changed to arr[i++] from arr
}
答案 1 :(得分:0)
尝试一下;
while ((line = br.readLine()) != null)
{
arr[i] = line;
System.out.println(i + "." + arr[i++] + "\n");
}
输出为; 0. 1 12 123 1234 12345 // 0.是索引