繁体   English   中英

如何获取Java以将文本文件存储在2D数组中并在所述数组中定位特定点?

[英]How do I get java to store a text file in a 2D array and locate a specific point in said array?

我正在为此程序使用Eclipse,而我想做的就是让程序读取文本文件“ ClimateDataA”,然后查找温度首次低于80度的日期,我必须使用&循环。

这是文本文件“ ClimateDataA”包含的内容:

14939 20140801 86

14939 20140802 90

14939 20140803 93

14939 20140804 87

14939 20140805 93

14939 20140806 83

14939 20140807 81

14939 20140808 83

14939 20140809 85

14939 20140810 85

14939 20140811 80

14939 20140812 81

14939 20140813 87

14939 20140814 89

14939 20140815 76

14939 20140816 87

14939 20140817 92

14939 20140818 91

14939 20140819 93

14939 20140820 96

14939 20140821 90

14939 20140822 93

14939 20140823 95

14939 20140824 91

14939 20140825 83

14939 20140826 81

14939 20140827 77

14939 20140828 82

14939 20140829 75

14939 20140830 87

14939 20140831 92

第1列是一个ID号。 第2列是日期。 第三栏是温度。

到目前为止,这是我的代码:

import java.util.Scanner;
import java.io.*;


public class ClimateSummary
{   
    public static void main (String [] args) throws FileNotFoundException
    {

    Scanner userInfo = new Scanner (new File("MyInfo.txt"));
    Scanner weatherData = new Scanner (new File("ClimateDataA.txt"));

    String userName = userInfo.nextLine();
    String userBirthPlace = userInfo.nextLine();
    int userBirthDay = userInfo.nextInt();
    String userBirthMonth = userInfo.next();
    int userBirthYear = userInfo.nextInt();
    String dataLocation = userInfo.next();

    int [][] myArray = new int[31][3];

    for (int i = 0; i < myArray.length; i++) {

    }


    System.out.println("Welcome " + userName + "! This is the Super Computing Weathernator 3000!");
    System.out.println();
    System.out.println("Name: " + userName);
    System.out.println("Date of birth: " + userBirthDay + " " + userBirthMonth + " " + userBirthYear );
    System.out.println("Place of birth: " + userBirthPlace);
    System.out.println("Data collected at: " + dataLocation);
    System.out.println();
    System.out.println("The first day under 80 degrees is ");


    userInfo.close();

 }
}

没有这些代码行,程序就可以运行。

Scanner weatherData = new Scanner (new File("ClimateDataA.txt"));

 int [][] myArray = new int[31][3];

    for (int i = 0; i < myArray.length; i++) {

}

这是我想要获得的输出:

欢迎埃里克·霍恩伯格! 这是超级计算Weathernator 3000!

姓名:埃里克·霍恩伯格

生日:1960年2月18日

出生地:内布拉斯加州哥伦布

数据收集于:哥伦布

80度以下的第一天是15/08/2014

前五行使用的是“ MyInfo”文本文件中的数据。

我将不得不解析以获取与输出中的日期完全相同的日期,但是我只需要获取所需数据的帮助即可。

谁能帮我正确地做到这一点? 谢谢你的时间!

您应该将文件中的每一行都视为一个单独的字符串。 然后,您可以将这些字符串分成多个部分(id,日期,温度),并从每一列中获取数据。 这是有关拆分字符串的一个较旧的问题: 如何在Java中拆分字符串

这是有关如何从文件中读取行的信息Java从文件中读取行

所以放在一起:

  • 循环浏览文件中的行
  • 将每行视为您分割的字符串(在您的情况下为空白)以获取数据
  • 如果温度值低于先前找到的最小值,则将温度值保存在一些变量中

希望至少能帮助您入门!

另外还有一件事:由于您使用的是Java,因此您可能要考虑使用对象数组代替2D数组来存储数据,类似于:

public Class WeatherData {
   private int id;
   private Date date;
   private int temperature;

   /** Getters, setter, contructor etc */ 
}

这将使您的程序在需要时更易于阅读和修改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM