[英]How to add both ints and strings from the same text file into different arrays Java
我需要制作一个程序,将文本文件中的值存储到多个数组中。 文本文件如下所示:
1995年6月987 65艾莉森
1995年7月973 85艾琳
1995年8月929120费利克斯
1995年8月968 95温贝托
1995年8月965 95 Iris
我需要将每列添加到单独的数组中。 我如何告诉程序专门针对一列将其添加到数组,然后添加到另一列?
我试图这样做:
File fileName = new File ("hurricanedata.txt");
Scanner inFile = new Scanner(fileName);
while (inFile.hasNextLine()) {
index++;
inFile.nextLine();
}
hurricaneNames = new String [index];
years = new int [index];
months = new String [index];
pressures = new int [index];
windSpeeds = new int [index];
while (inFile.hasNext()) {
int i = 0;
years [i] = inFile.nextInt();
months [i] = inFile.next();
pressures [i] = inFile.nextInt();
windSpeeds [i] = inFile.nextInt();
hurricaneNames [i] = inFile.next();
System.out.println(years[i]); //print statement to test
i++;
}
但是print语句不打印任何内容。
完成此操作后:
while (inFile.hasNextLine()) {
index++;
inFile.nextLine(); // HERE
}
您的文件现在正在文件末尾读取。 因此,next()不会像您希望的那样从文件的开头开始。
您只需使用扫描仪重新打开文件即可:
File fileName = new File ("hurricanedata.txt");
Scanner inFile = new Scanner(fileName);
while (inFile.hasNextLine()) {
index++;
inFile.nextLine(); // Increments the line of the file that you're reading, so the
// next "next()" you call starts at the >next< line of the file.
}
inFile.close();
inFile = new Scanner(fileName); // NOTE: Restarts the Scanner, ensuring that it
// starts reading from the beginning of the file again.
hurricaneNames = new String [index];
years = new int [index];
months = new String [index];
pressures = new int [index];
windSpeeds = new int [index];
int i = 0; // NOTE: This should be OUT of the loop, so that i increments
// and doesn't restart to 0.
while (inFile.hasNext()) {
years [i] = inFile.nextInt();
months [i] = inFile.next();
pressures [i] = inFile.nextInt();
windSpeeds [i] = inFile.nextInt();
hurricaneNames [i] = inFile.next();
System.out.println(years[i]); //print statement to test
i++;
}
在第一个while循环中,您读到file的末尾 。 因此,在第二个while循环中,由于指针位于文件末尾,因此您无需读取任何内容。
您只能在一个循环中执行此操作,如下所示:
File fileName = new File ("hurricanedata.txt");
Scanner inFile = new Scanner(fileName);
List<String> hurricaneNames = new ArrayList<>();
List<Integer> years = new ArrayList<>();
List<String> months = new ArrayList<>();
List<Integer> pressures = new ArrayList<>();
List<Integer> windSpeeds = new ArrayList<>();
while (inFile.hasNextLine()) {
String[] tempArray = inFile.nextLine().split(" ");
years.add(Integer.parseInt(tempArray[0]));
months.add(tempArray[1]);
pressures.add(Integer.parseInt(tempArray[2]));
windSpeeds.add(Integer.parseInt(tempArray[3]));
hurricaneNames.add(tempArray[4]);
}
在第一个循环中,您测试了inFile.nextLine()
。 因此,当您使用第二个循环时, inFile.nextLine()
已耗尽。
您的第一个循环仅计算文件中的行数。 然后,您的第二个循环读取数据,但是到那时该文件位于末尾,因此没有要读取的内容。
让我提出一些建议。 首先,调用行数index
会产生误导。 该变量从不用作索引,而仅用作数组大小。 lineCount
。
其次,您可以使用Java NIO和Streams使用更简单的代码来获取文件中的行数。
您的其余代码大部分都可以保持不变。 索引变量i
需要在while
循环之外。 并且最好将Scanner
包装在try-with-resources循环中。
final Path filePath = Paths.get("hurricanedata.txt");
final int lineCount;
try (Stream<String> lines = Files.lines(filePath)) {
lineCount = (int)lines.count();
}
hurricaneNames = new String[lineCount];
years = new int[lineCount];
months = new String[lineCount];
pressures = new int[lineCount];
windSpeeds = new int[lineCount];
try (Scanner inFile = new Scanner(filePath)) {
int i = 0;
while (inFile.hasNext()) {
years[i] = inFile.nextInt();
months[i] = inFile.next();
pressures[i] = inFile.nextInt();
windSpeeds[i] = inFile.nextInt();
hurricaneNames[i] = inFile.next();
System.out.println(years[i]); //print statement to test
i++;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.