gpt4 book ai didi

java - 如何将文本文件放入数组并搜索特定值但仅限于一维数组

转载 作者:太空宇宙 更新时间:2023-11-04 09:49:10 24 4
gpt4 key购买 nike

正如标题所示,我正在尝试获取此 Weather Station Data File进入数组并能够通过菜单系统搜索文件中的特定值。我构建了菜单系统,但不知道如何将文件导入到这样的数组或一系列数组中。我遇到的唯一问题是我只能使用一维数组来存储数据。我的教科书和教授都没有任何帮助。这是我到目前为止所拥有的:

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]

关于java - 如何将文本文件放入数组并搜索特定值但仅限于一维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55015272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com