我正在尝试扫描文本文件并将字符串存储到数组中,同时忽略第一行。当我运行代码时,它有一个空白点,并且数组缺少一个字符串。是否有办法扫描文件并让它跳过 int 值并包含所有字符串?
文件 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]
最佳答案
尝试每次读取整行,包括初始整数值:
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));
关于java - 有没有办法在存储字符串时扫描文件并忽略 int 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55350146/