java - 如何将文本文件放入数组并搜索特定值但仅限于一维数组
问题描述
正如标题所说,我正在尝试将这个气象站数据文件放入一个数组中,并能够通过菜单系统在文件中搜索特定值。我构建了菜单系统,但不知道如何将文件导入这样的数组或一系列数组。我唯一的问题是我仅限于对数据使用一维数组。我的教科书和教授都没有任何帮助。这是我到目前为止所拥有的:
import java.io.*;
import java.util.*;
public class VidrioTomas2 {
public static void main(String[] args)
throws FileNotFoundException {
System.out.println("Please give the data titled 'Bellevue College Weather Station Data.txt'.");
System.out.println("The data must be in the 10 minute data format.");
Scanner console = new Scanner (System.in);
System.out.print("Please type the file name: ");
String name = console.nextLine(); //Uses the user input to find the file.
if (name.equals("Bellevue College Weather Station Data.txt")) { //Needs to type the.txt and in the way the program says it is.
System.out.println("\nFile name secured. Proceeding to next step.");
File input = new File(name); //Stores the file into a value that can used in other methods.
if(input.length() == 67481){
System.out.println("\nLength of file: " + input.length() + "\nConfirmation of data. Proceeding to next step."); //Used as a confirmation that the file is true and working.
System.out.println("The following options are available. Please select one of them.");
System.out.println("1) Peak Wind Speed"
+ "\n2) Maximum Temperature"
+ "\n3) Humidity and Temperature"
+ "\n4) Solar Energy"
+ "\n5) End Program");
Scanner secondConsole = new Scanner (System.in);
System.out.print("Your selection: ");
int choice = secondConsole.nextInt();//Uses the user input to start up one of the options.
switch (choice) {
case 1:
System.out.println("Starting Peak Wind Speed Calculation. . .");
peakWindSpeedCalc(input);
break;
case 2:
System.out.println("Starting Maximum Temperature Calculation. . .");
maximumTempCalc(input);
break;
case 3:
System.out.println("Starting Humidity and Temperature Calculation. . .");
humidAndTempCalc(input);
break;
case 4:
System.out.println("Starting Solar Energy Calculation. . .");
solarEnergyCalc(input);
break;
case 5:
System.out.println("Ending program. . .");
break;
}
} else {
System.out.println("Inproper file. Ending program. . ."); //Kills if the length is not the same. The file I had had a length of 67481.
}
} else {
System.out.println("\nInproper name or type. Ending program. . ."); //Kills if file name is wrong. Caps sensitive.
}
}
public static void peakWindSpeedCalc(File rawData) {
System.out.println("Placeholder.exe is active. peakWindSpeedCalc is linked correctly.");
}
public static void maximumTempCalc(File rawData) {
System.out.println("Placeholder.exe is active. maximumTempCalc is linked correctly.");
}
public static void humidAndTempCalc(File rawData) {
System.out.println("Placeholder.exe is active. humdAndTempCalc is linked correctly.");
}
public static void solarEnergyCalc(File rawData) {
System.out.println("Placeholder.exe is active. solarEnergyCalc is linked correctly.");
}
}
解决方案
看起来你有文件对象:
File input = new File(name);
接下来,您需要对其进行解析。从你的截图来看,前几行并不是完全有用,所以你可以跳过它们,然后split()
在有用的行上使用:
String[] firstline = Files.readAllLines(file.toPath()).get(3).split("\\W");
现在你有了一个数组,其中包含第一行的每个值。你可以设置常量来索引到这个列表中,所以你可以像引用它一样引用它firstline[BAR]
。
如果要将所有内容存储在单个数组中,则可以像这样对其进行索引,例如:
data[line * numColumns + col]
推荐阅读
- python - 无法在 python selenium 中使用 xpath 和 id 定位 iframe
- c# - SQLLite - 选择两个日期之间的预订
- android - 为什么android Intent ACTION_IMAGE_CAPTURE 结果代码总是0(CANCELLED)
- java - 无法反序列化 json:MismatchedInputException。使用静态类
- python - 读取文件行数的更快方法
- visual-studio - 何时使用 `silent` 代码分析严重性?
- c# - 在不破坏游戏对象的情况下调用方法或发送回调
- python - Python Pandas:从具有多个(合并单元格)标题的 Excel 文件创建数据框
- reactjs - TypeScript 3.7 现在对所有单个(无效)道具抛出错误,而不是像以前那样对它们进行分组
- c# - 为什么我不能在 ObservableCollection 中使用数组值进行绑定?