我有一个项目,我必须根据给定的月份或日期查找数据。如果为null,则返回该年/日/月的所有数据。我得到了第一个读取文件并将数据放入数组的方法;每行是
格式的特定时间的数据#site_code年月日小时分秒值value_unc nvalue纬度经度海拔高度进度_高度仪qcflag
数据字符串的示例是:
BRW 1973 1 1 0 0 0 -999.99 -99.99 0 71.323 -156.611 27.0 11.0 16.0 NA * ..
BRW 1973 1 2 0 0 0 -999.99 -99.99 0 71.323 -156.611 27.0 11.0 16.0 NA * ..
问题是,我不知道如何获取第一个方法数组的部分内容,以便收集所调用的年/日/月的所有数据。
这是我的代码:
public ArrayList<CO2Data> loadData(String filename) throws FileNotFoundException {
ArrayList array = new ArrayList();
String input = "";
try {
Scanner scan = new Scanner(new File("input.txt"));
while ((input = scan.next()) != null){
input = scan.next();
if (input == "BRW"){
array.add(scan.nextLine());
}
}
scan.close();
}
catch (FileNotFoundException exception)
{
System.out.println("could not find file");
}
return array;
}
@Override
public ArrayList<CO2Data> getData(Integer year, Integer month, Integer day) {
ArrayList array = null;
for(int i = 0; != null; i++){
if (year == nextLoadData || year == null){
if (month == nextLoadData || month == null){
if (day == nextLoadData || day == null){
array.add();
}}
}
return array;
}
答案 0 :(得分:0)
我将添加完整的解决方案,以便您可以学习和更改它。
首先,我们使用CO2Data类来保存一行数据。也许你可以查看Lesson: Object-Oriented Programming Concepts
public class CO2Data
{
private String site_code;
private String year;
private String month;
private String day;
private String hour;
private String minute;
private String second;
private String value;
private String value_unc;
private String nvalue;
private String latitude;
private String longitude;
private String altitude;
private String elevation;
private String intake_height;
private String instrument;
private String qcflag;
public CO2Data(){
}
public String getSite_code ( )
{
return site_code;
}
public void setSite_code ( String site_code )
{
this.site_code = site_code;
}
public String getYear ( )
{
return year;
}
public void setYear ( String year )
{
this.year = year;
}
public String getMonth ( )
{
return month;
}
public void setMonth ( String month )
{
this.month = month;
}
public String getDay ( )
{
return day;
}
public void setDay ( String day )
{
this.day = day;
}
public String getHour ( )
{
return hour;
}
public void setHour ( String hour )
{
this.hour = hour;
}
public String getMinute ( )
{
return minute;
}
public void setMinute ( String minute )
{
this.minute = minute;
}
public String getSecond ( )
{
return second;
}
public void setSecond ( String second )
{
this.second = second;
}
public String getValue ( )
{
return value;
}
public void setValue ( String value )
{
this.value = value;
}
public String getValue_unc ( )
{
return value_unc;
}
public void setValue_unc ( String value_unc )
{
this.value_unc = value_unc;
}
public String getNvalue ( )
{
return nvalue;
}
public void setNvalue ( String nvalue )
{
this.nvalue = nvalue;
}
public String getLatitude ( )
{
return latitude;
}
public void setLatitude ( String latitude )
{
this.latitude = latitude;
}
public String getLongitude ( )
{
return longitude;
}
public void setLongitude ( String longitude )
{
this.longitude = longitude;
}
public String getAltitude ( )
{
return altitude;
}
public void setAltitude ( String altitude )
{
this.altitude = altitude;
}
public String getElevation ( )
{
return elevation;
}
public void setElevation ( String elevation )
{
this.elevation = elevation;
}
public String getIntake_height ( )
{
return intake_height;
}
public void setIntake_height ( String intake_height )
{
this.intake_height = intake_height;
}
public String getInstrument ( )
{
return instrument;
}
public void setInstrument ( String instrument )
{
this.instrument = instrument;
}
public String getQcflag ( )
{
return qcflag;
}
public void setQcflag ( String qcflag )
{
this.qcflag = qcflag;
}
@Override
public String toString ( )
{
return "CO2Data [site_code=" + site_code + ", year=" + year + ", month=" + month + ", day="
+ day + ", hour=" + hour + ", minute=" + minute + ", second=" + second + ", value="
+ value + ", value_unc=" + value_unc + ", nvalue=" + nvalue + ", latitude="
+ latitude + ", longitude=" + longitude + ", altitude=" + altitude + ", elevation="
+ elevation + ", intake_height=" + intake_height + ", instrument=" + instrument
+ ", qcflag=" + qcflag + "]";
}
}
1)从文件中读取数据并将其保存在CO2数据列表中。
2)从上一个匹配结果的列表中获取数据。
3)打印:D
注意:不要忘记更改 PathToFile 。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test
{
public static void main ( String [ ] args )
{
List<CO2Data> readList = loadData ( "PathToFile" );
if ( readList != null )
{
List<CO2Data> dataFound = getData ( readList , "1973" , "1" , "1" );
if( dataFound != null )
{
//separator
System.out.println ( "----------------------------------------" );
//print results
for(int i = 0; i < dataFound.size ( ); i++)
{
System.out.println ( dataFound.get ( i ).toString ( ) );
}
}
}
}
//Read data from the file
public static List<CO2Data> loadData(String filename)
{
List<CO2Data> listCO2Data = new ArrayList < CO2Data >( );
try {
Scanner scan = new Scanner(new File(filename));
while (scan.hasNextLine ( ))
{
CO2Data data = new CO2Data ( );
data.setSite_code(scan.next());
data.setYear(scan.next());
data.setMonth(scan.next());
data.setDay(scan.next());
data.setHour(scan.next());
data.setMinute(scan.next());
data.setSecond(scan.next());
data.setValue(scan.next());
data.setValue_unc(scan.next());
data.setNvalue(scan.next());
data.setLatitude(scan.next());
data.setLongitude(scan.next());
data.setAltitude(scan.next());
data.setElevation(scan.next());
data.setIntake_height(scan.next());
data.setInstrument(scan.next());
data.setQcflag(scan.next());
//print adds just for informational purposes
System.out.println ( data.toString ( ) );
listCO2Data.add ( data );
}
scan.close();
}
catch (FileNotFoundException exception)
{
exception.printStackTrace ( );
System.out.println("Could not find file");
}
return listCO2Data.isEmpty ( ) ? null : listCO2Data;
}
//get data with matching dates
public static List < CO2Data > getData(List<CO2Data> data, String year, String month, String day)
{
List < CO2Data > dataFound = new ArrayList<> ( );
for(CO2Data current : data)
{
if( current.getYear ( ).equals ( year ) &&
current.getMonth ( ).equals ( month ) &&
current.getDay ( ).equals ( day ))
{
dataFound.add ( current );
}
}
return dataFound.isEmpty ( ) ? null : dataFound ;
}
}
BRW 1973 1 1 0 0 0 -999.99 -99.99 0 71.323 -156.611 27.0 11.0 16.0 NA *..
BRW 1973 2 1 0 0 0 -999.99 -99.99 0 71.323 -156.611 27.0 11.0 16.0 NA *..
BRW 1973 3 1 0 0 0 -999.99 -99.99 0 71.323 -156.611 27.0 11.0 16.0 NA *..
BRW 1973 4 1 0 0 0 -999.99 -99.99 0 71.323 -156.611 27.0 11.0 16.0 NA *..
BRW 1973 5 1 0 0 0 -999.99 -99.99 0 71.323 -156.611 27.0 11.0 16.0 NA *..
BRW 1973 1 1 0 0 0 -999.99 -99.99 0 71.323 -156.611 27.0 11.0 16.0 NA *..
CO2Data [site_code=BRW, year=1973, month=1, day=1, hour=0, minute=0, second=0, value=-999.99, value_unc=-99.99, nvalue=0, latitude=71.323, longitude=-156.611, altitude=27.0, elevation=11.0, intake_height=16.0, instrument=NA, qcflag=*..]
CO2Data [site_code=BRW, year=1973, month=2, day=1, hour=0, minute=0, second=0, value=-999.99, value_unc=-99.99, nvalue=0, latitude=71.323, longitude=-156.611, altitude=27.0, elevation=11.0, intake_height=16.0, instrument=NA, qcflag=*..]
CO2Data [site_code=BRW, year=1973, month=3, day=1, hour=0, minute=0, second=0, value=-999.99, value_unc=-99.99, nvalue=0, latitude=71.323, longitude=-156.611, altitude=27.0, elevation=11.0, intake_height=16.0, instrument=NA, qcflag=*..]
CO2Data [site_code=BRW, year=1973, month=4, day=1, hour=0, minute=0, second=0, value=-999.99, value_unc=-99.99, nvalue=0, latitude=71.323, longitude=-156.611, altitude=27.0, elevation=11.0, intake_height=16.0, instrument=NA, qcflag=*..]
CO2Data [site_code=BRW, year=1973, month=5, day=1, hour=0, minute=0, second=0, value=-999.99, value_unc=-99.99, nvalue=0, latitude=71.323, longitude=-156.611, altitude=27.0, elevation=11.0, intake_height=16.0, instrument=NA, qcflag=*..]
CO2Data [site_code=BRW, year=1973, month=1, day=1, hour=0, minute=0, second=0, value=-999.99, value_unc=-99.99, nvalue=0, latitude=71.323, longitude=-156.611, altitude=27.0, elevation=11.0, intake_height=16.0, instrument=NA, qcflag=*..]
----------------------------------------
CO2Data [site_code=BRW, year=1973, month=1, day=1, hour=0, minute=0, second=0, value=-999.99, value_unc=-99.99, nvalue=0, latitude=71.323, longitude=-156.611, altitude=27.0, elevation=11.0, intake_height=16.0, instrument=NA, qcflag=*..]
CO2Data [site_code=BRW, year=1973, month=1, day=1, hour=0, minute=0, second=0, value=-999.99, value_unc=-99.99, nvalue=0, latitude=71.323, longitude=-156.611, altitude=27.0, elevation=11.0, intake_height=16.0, instrument=NA, qcflag=*..]
我还在代码中添加了一些注释。 如果您对代码有任何疑问,请不要犹豫,我会相应地编辑我的答案。 祝你好运。
答案 1 :(得分:0)
我怀疑你需要在这里做一些重新设计,以便让自己更轻松。我会提出一些建议,如果您在理解方面有困难,可以在评论中告诉我。
您已经有一个CO2Data
类,可能会存储来自文件单行的信息。理想情况下,它会有一个方法可以获取输入字符串并转换为其中一个对象。我们假设它看起来像:
class CO2Data {
private static final Pattern PATTERN = Pattern.compile("(\\s{3}) (\\d{4})");
private final String siteCode;
private final LocalDataTime timeOfReading;
private CO2Data(String siteCode, int year) {
this.siteCode = siteCode;
this.timeOfReading = LocalDataTime.of(year, 1, 1, 1, 1);
public static CO2Data createFromLine(String line) {
Matcher matcher = PATTERN.match(line);
if (matcher.matches()) {
String siteCode = matcher.group(1);
int year = Integer.parseInt(matcher.group(2));
return new CO2Data(siteCode, year);
} else {
throw new IllegalArgumentException("Incorrectly formatted line");
}
}
}
为了清晰起见,我已将其简化为网站代码和年份,但您可以相当简单地扩展到其他领域。
从文件中读取的方法应该返回这些对象的列表:
public List<CO2Data> getDataFromFile(Path filepath) throws IOException {
return Files.lines(filepath)
.map(CO2Data::createFromLine)
.collect(Collectors.toList());
}
然后按年和月过滤的方法可以列出这些对象:
public List<CO2Data> search(List<CO2Data> list, Integer year) {
return list.stream()
.filter(d -> year == null || d.getTime().getYear() == year)
.collect(Collectors.toList());
}
请注意,我在这里使用了Java 8功能。