有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将一个方法数组的一部分实现为一个新方法

我有一个项目,我必须根据给定的月份、年份或日期查找数据。如果为空,则返回该年/日/月的所有数据。我得到了第一种读取文件并将数据放入数组的方法;每一行都是特定时间的数据,格式为

#站点_代码年月日时分秒值_unc值经纬度海拔高度入口_高度仪表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;
}

共 (2) 个答案

  1. # 1 楼答案

    我将添加完整的解决方案,以便您可以研究和更改它

    首先,我们有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 + "]";
        }
    
    
    
    }
    

    实现了方法的MainClass:

    1) read data from the file and save it on a CO2Data List.

    2) get data from the previous list matching the results.

    3) print :D

    Note: Do not forget to change PathToFile with yours.

    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 ;
        }
    }
    

    输入。txt:

    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=*..]
    

    我还在代码中添加了一些注释。 如果你对代码有任何疑问,请不要犹豫,我会相应地编辑我的答案。 祝你好运

  2. # 2 楼答案

    我想你需要在这里做一些重新设计,让事情变得更简单。我会提出一些建议,如果你在理解上有困难,你可以在评论中告诉我

    您已经有了一个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());
    }
    

    请注意,我在这里使用了Java8特性