读取参数数组中的变量时遇到问题java
我正在读取一个csv文件,该文件读取国家/地区名称,并在特定年份为特定国家/地区收取订阅数据。以下是一个csv示例文件:读取参数数组中的变量时遇到问题java
CountryName,1960,1961,1962,1963,1964,1965,1966,1967,1968,1969,1970,1971,1972,1973,1974,1975,1976,1977,1978,1979,1980,1981,1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012
Aruba,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.029310471,0,0,2.138784453,3.605985937,3.98141538,6.16435217,13.48254011,16.50927821,57.05427692,65.05605558,72.10431377,99.64250268,103.3849507,108.1325002,112.2180618,119.2038996,126.2103374,129.72824,0,131.8565401
Andorra,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1.307211734,1.278625641,1.250259142,4.424155104,8.538444783,13.44671556,22.12730607,32.14530928,35.99902139,43.27794118,45.77115817,68.60251444,73.82494308,79.48487497,84.27763597,78.1171579,80.2836099,82.06181111,84.06818386,83.53432222,81.50204186
Afghanistan,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.112598381,0.865196277,2.498055472,4.826865367,9.833164022,17.71624331,29.22037376,37.89493697,45.77817474,60.32631999,60.35299258
我能够正常读取文件。我创建了2个课程。 1类叫class subscriptionYear
,读取我调用int year
的年份,以及读取每个统计数据的变量double subscription
。第二类叫Class Country
。它读取名为String countryNames
的国家和称为SubscriptionYear[] subscriptions
的1d阵列,该阵列包含该国家的所有订阅数据。我在名为addSubscriptionYear(int year, double countryData)
的课程国家中有两种方法,它们读取我要遵循测试课程指南class TestCountry
的指导方针。我的程序读取文件但输出错误的总和。基本上它只读了2012年,而不是1960年到2012年之间的整个时期。所以当我这样做时,它只输出2012年的统计数据,而不是1960年到2012年的全部数据。我的问题似乎是从addSubscriptionYear method
插入年和数据到class Subscription
,但它只读取2012年,而不是从1960年到2012年的整个时期,当我尝试调试它。我必须遵循TestCountry class
的指导原则,因为这是要求。但我的功能方法addSubscriptionYear()
是问题所在。有人可以帮我解决一些问题,或者如何将每年的数据和数据存储在对象数组中。下面是我的文件
类订阅:
public class SubscriptionYear {
private int year;
private double subscriptions;
public SubscriptionYear(int year,double subscriptions)
{
//this.year = year;
//this.subscriptions = subscriptions;
setYear(year);
setSubscription(subscriptions);
}
public void setYear(int Year)
{
this.year= Year;
}
public void setSubscription(double value)
{
this.subscriptions = value;
}
public int getYear()
{
return year;
}
public double getSubscription()
{
return subscriptions;
}
public String toString()
{
return "Number of Subscriptions: "+subscriptions;
}
}
类国家:
public class Country {
private String countryNames;
private SubscriptionYear[] subscriptions;
private int size;
public Country(String country, int arraylength)
{
this.countryNames = country;
this.size = arraylength;
subscriptions = new SubscriptionYear[size];
}
//seems to be where i am having the problem
//only reads 2012 and not 1960. seems to be my problem
public void addSubscriptionYear(int year, double subscription)
{
for(int i=0;i<subscriptions.length;i++)
{
subscriptions[i]= new SubscriptionYear(year, subscription);
}
//System.out.print(subscriptions[0].getYear()+"\t");
}
public double getNumSubscriptionsForPeriod(int start, int end)
{
double sum = 0;
int head = subscriptions[0].getYear()-start;
int tail = end-start;
if(head>=0&&end<subscriptions.length)/check if out of bounds.
{
for(int k=head;k<=tail;k++)
{
sum += subscriptions[k].getSubscription();
}
} else{ sum =-1;}
return sum;
}
public String toString()
{
System.out.print(countryNames+"\t");
for(SubscriptionYear s: subscriptions)
{
//System.out.print(countryNames+"\t");
System.out.print(s.getSubscription()+"\t");
}
System.out.println();
return "";
}
}
Tesfile:
/**
* Tests the Country class by creating multiple objects.
* Creates one object of type CSVReader class, which reads input from a CSV file. Uses
* the attributes stored in CSVReader object to create objects of type Country class.
*/
public class TestCountry
{
/**
* Includes test examples for class Country.
*/
public static void main(String[] args)
{
// Create and set objects of type Country
//
final String FILENAME = "data/cellular.csv"; // Directory path for Mac OS X
//final String FILENAME = "data\cellular.csv"; // Directory path for Windows OS (i.e. Operating System)
final int NUM_COUNTRIES_TO_TEST = 3; // Note: Include test cases in addition to 3
// Parse the CSV data file
//
CSVReader parser = new CSVReader(FILENAME);
String [] countryNames = parser.getCountryNames();
int [] yearLabels = parser.getYearLabels();
double [][] parsedTable = parser.getParsedTable();
// Create and set objects of type Country
//
Country [] countries;
//countries = new Country[NUM_COUNTRIES_TO_TEST]; // Note: Use this for initial testing of your implementation.
countries = new Country[countryNames.length];
Country current;
for (int countryIndex = 0; countryIndex < countries.length; countryIndex++)
{
int numberOfYears = yearLabels.length; // OR numberOfYears = dataTable[countryIndex].length;
current = new Country(countryNames[countryIndex], numberOfYears);
for (int yearIndex = 0; yearIndex < numberOfYears; yearIndex++)
{
double [] allSubscriptions = parsedTable[countryIndex];
double countryData = allSubscriptions[yearIndex];
current.addSubscriptionYear(yearLabels[yearIndex], countryData);
}
countries[countryIndex] = current;
}
//for(int i =0;i<countries.length;i++)
//{
//System.out.print(countries[8]);
//}
//System.out.println();
System.out.printf(countryNames[0] + " (1960 to 2012): %.2f \n", countries[0].getNumSubscriptionsForPeriod(1960,2012));
// the output is 131.63 and should be 1170.50
System.out.printf(countryNames[100] + " (1960 to 2012): %.2f \n", countries[100].getNumSubscriptionsForPeriod(1960,2012));
// the output is 112.67 and should be 1246.58
}
}
它输出131.63阿鲁巴,而不是1170.50。它基本上只读取2012年的统计数据,而不是整个1960年到2012年的时间。我需要帮助修复类Country中的addSubscriptionYear方法。
您只保留最后一个日期,因为您在addSubscriptionYear()
方法中放置了一个for循环,并且覆盖了所有先前的值,因此请移除for循环,并通过添加并保持跟踪当前索引INT您Country
类,将增加每次调用该方法addSubscriptionYear()
int lastPos = 0;
public void addSubscriptionYear(int year, double subscription)
{
subscriptions[lastPos]= new SubscriptionYear(year, subscription);
lastPos++;
}
cldü请示例 – user2738145 2014-10-17 19:15:21
brso05询问了代码。但是,您是否尝试过我提出的解决方案? – 2014-10-17 19:16:47
在这里你去我已经添加了它 – 2014-10-17 19:19:40
哪里是getCountryNames))getParsedTable()代码(getYearLabels(和。 – brso05 2014-10-17 19:02:55
@ brso05发布了代码ü询问了 – user2738145 2014-10-17 19:16:26