Java课程作业问题
我是一名本科生,我正在寻找帮助,帮助我解决为我的课程编写的代码故障Java课程作业问题,java,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
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");
}
}
和天气读数:
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;
}
}
我的建议是检查从文件中读取的行数组的长度
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]));
你确定WeatherData.getData()代码>返回什么?返回的字符串中有“
”,“。请告诉我您导入的jar,以便在问题没有解决时我可以更正。
我想你还不清楚你在做什么。你能把这个例子简化一点,并提供样本数据来尝试吗?你为什么这样做:
String[]split=line[I]。split(“,”)
?您在哪里使用split
?@JacobBarnes是的,数组的第一行包含站点id、站点名称、纬度、经度等标题信息。看起来您指的是新气象站(split[0]、split[1]、Double.parseDouble(split2])、Double.parseDouble(split[3]);使用拆分而不是行。@isaace这是一个逗号分隔的字符串,我试图使用字符串[]拆分=行[i]来拆分它。拆分(“,”;
WeatherStation z = new WeatherStation(split[0], split[1], Double.parseDouble(split[2]), Double.parseDouble(split[3]));
String line[] = WeatherData.getData();