2014-10-17 56 views
-1

我正在讀取一個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方法。

+0

哪裏是getCountryNames))getParsedTable()代碼(getYearLabels(和。 – brso05 2014-10-17 19:02:55

+0

@ brso05發佈了代碼ü詢問了 – user2738145 2014-10-17 19:16:26

回答

1

您只保留最後一個日期,因爲您在addSubscriptionYear()方法中放置了一個for循環,並且覆蓋了所有先前的值,因此請移除for循環,並通過添加並保持跟蹤當前索引INT您Country類,將增加每次調用該方法addSubscriptionYear()

int lastPos = 0; 
public void addSubscriptionYear(int year, double subscription) 
{ 
    subscriptions[lastPos]= new SubscriptionYear(year, subscription); 
    lastPos++; 
} 
+0

cldü請示例 – user2738145 2014-10-17 19:15:21

+0

brso05詢問了代碼。但是,您是否嘗試過我提出的解決方案? – 2014-10-17 19:16:47

+0

在這裏你去我已經添加了它 – 2014-10-17 19:19:40