我正在尝试扫描文本文件,并将字符串存储到数组中,而忽略第一行。当我运行代码时,它有一个空白点,并且数组缺少字符串。无论如何,有没有要扫描的文件,并让它跳过int值并包含所有字符串?
File file = new File(“ input.txt”);
Scanner scan = new Scanner(file);
int size = scan.nextInt();
String[] blocks = new String[size];
for (int i = 0; i < blocks.length; i++) {
blocks[i] = scan.nextLine();
}
System.out.println(Arrays.toString(blocks));
[,MXTUAS,OQATGE,REWMNA,MBDFAC]
答案 0 :(得分:0)
尝试每次读取整行,包括初始整数值:
File file = new File("input.txt");
Scanner scan = new Scanner(file);
int size = Integer.parseInt(scan.nextLine());
String[] blocks = new String[size];
for (int i = 0; i < blocks.length; i++) {
blocks[i] = scan.nextLine();
}
System.out.println(Arrays.toString(blocks));