Java课程问题

时间:2017-11-15 16:42:17

标签: java

我是一名本科生,我正在寻找帮助,帮助我解决我为课程编写的代码问题。

import weather.WeatherData;
import Weather.data.classes.WeatherReading;
import Weather.data.classes.WeatherStation;

/**
 * QUESTION 01
 * 
 * If you decide to answer question 01 then the main method below should be used as the entry point for your application
 * You may use as many other classes as you feel necessary as long as all code is supplied 
 * 
 * Remember to add -Xmx1024m to the VM arguments using menu run --> run configurations in eclipse
 */
public class Answer01 {
    public static void main(String[] args) {
        System.out.println("Question 01");
        /*
         * Add your code below
         */
        WeatherStation x = new WeatherStation("site", "a", 1.1, 1.0);
        WeatherReading y = new WeatherReading(1,2,3,4,5,6);
        String line[] = WeatherData.getData();
        ArrayList<WeatherStation>weatherSize = new ArrayList<>();
        for(int i = 1; i < line.length; i++){
        String [] split = line[i].split(",");
        WeatherStation z = new WeatherStation(line[0], line[1], Double.parseDouble(line[2]), Double.parseDouble(line[3]));
        weatherSize.add(z);
        }

        System.out.println(weatherSize.size() + " test");

    }   
}

我遇到的问题是,weatherSize.size()正在打印0而不是正确的答案136

代码从名为weather.WeatherData的JAR文件中提取数据,我创建了两个具有getter和setter以及toString()方法的类,称为WeatherStationWeatherReading

jar文件是名为WeatherData.jar的库项目,它记录了在英国不同位置记录的天气数据。

String [] weatherData = WeatherData.getData();

返回一个字符串数组,索引0处的第一个元素包含标题信息。

元素0:SiteId,SiteName,纬度,经度,年份,月份,日期,小时,WindSpeed,温度 要素1: 3002,BALTASOUND(3002),60.7490,-0.8540,2015,01,01,0,21,8.70

当我运行Answer01的代码时,它只是说:0 test

    package Weather.data.classes;

    public class WeatherStation {
    public String siteId;
    public String siteName;
    public double latitude;
    public double longitude;

    public WeatherStation(String siteId, String siteName, double latitude, double longitute){
        super();
        this.siteId = siteId;
        this.siteName = siteName;
        this.latitude = latitude;
        this.longitude = longitude;
        } 
    }        

WeatherReading

package Weather.data.classes;

public class WeatherReading {
    public int year;
    public int month;
    public int date;
    public int hour;
    public int windSpeed;
    public int temperature;

    public WeatherReading(int year, int month, int date, int hour, int windSpeed, int temperature){
        this.year = year;
        this.month = month;
        this.date = date;
        this.hour = hour;
        this.windSpeed = windSpeed;
        this.temperature = temperature;
    }    
}

2 个答案:

答案 0 :(得分:0)

我的建议是检查从文件中读取的行数组的长度

String line[] = WeatherData.getData();
System.out.println("Number of lines : " + line.length);

这样您就可以知道文件是否正确读取。 你也可以查看这一行吗

 WeatherStation z = new WeatherStation(line[0], line[1], Double.parseDouble(line[2]), Double.parseDouble(line[3]));

我认为应该是

WeatherStation z = new WeatherStation(split[0], split[1], Double.parseDouble(split[2]), Double.parseDouble(split[3]));

答案 1 :(得分:0)

 String line[] = WeatherData.getData();

你确定WeatherData.getData();还有什么东西吗?并且在它返回的String中退出","。请告诉您导入的jar,以便在问题无法解决时我能纠正。 我想你不清楚自己在做什么。