2014-10-09 60 views
3

需要幫助解決這個錯誤。我正在嘗試打印字符串countryNames的數組長度。在java中有文件讀取錯誤

Exception in thread "main" java.util.NoSuchElementException: No line found 
at java.util.Scanner.nextLine(Scanner.java:1516) 
at CSVReader.setUp(CSVReader.java:25) 
at CSVReader.<init>(CSVReader.java:16) 
at TestCSVReader.main(TestCSVReader.java:16) 

我正在閱讀一個cvs文件,我不斷收到上面的錯誤。下面是我的代碼:

import java.io.*; 
import java.util.Scanner; 
public class CSVReader { 
//String countryNames; 
String[] countryNames; 
int yearLabels; 
int[] yearNum; 
double cellularDataTable; 
double[][] tables; 
Scanner scan; 

public CSVReader(String filename)// throws FileNotFoundException 
{ 
    setUp(filename); 
} 
private void setUp(String filename) //throws FileNotFoundException 
{ 
    File file = new File(filename); 
    try{ 
     String input=""; 
     scan = new Scanner(file); 
     scan.nextLine(); 
     while((input=scan.nextLine())!=null) 
     { 
      String[] countryNames = input.split(","); 
      //int a = Integer.parseInt(countryNames[1]); 
      System.out.println(countryNames[0]); 
      //System.out.println(a); 
     } 
     scan.close(); 
    } 
    catch(FileNotFoundException e) 
    { 
     System.out.println(e); 
    } 
} 
    public String[] getCountryNames() 
{ 
    return countryNames; 
} 

下面是我的TestCSV類文件:

public class TestCSVReader { 

/** 
* Includes test examples for class CSVReader. 
*/ 
public static void main(String[] args) 
{ 
    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) 


    CSVReader parser = new CSVReader(FILENAME); 

    String [] countryNames = parser.getCountryNames(); 
    System.out.println(countryNames.length); 
    } 

我的CSV文件看起來像下面這樣:這是因爲我不希望打印出整個252個國家的彙總。基本上我接納252個國家並將它存儲在一個字符串數組中,並將它們存儲在一個int數組中,並將它們存儲在每個國家的蜂窩數據統計信息中。我是編程新手,想知道我是否正朝着正確的方向前進,如果不是,你將如何在一個字符串數組中輸入國家名稱,年代是int。並在2d雙數組中統計。

CVS文件看起來像這樣:

World Development Indicators 
Number of countries,252 
Country Name,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

在嘗試讀取它之前,您可能會先檢查是否有其他行。所以while((scanner.hasNextLine())'而不是'while((input = scan.nextLine())!= null)'可以幫助你。 – Tom 2014-10-09 08:38:48

+0

這是答案,所以只需將它寫爲答案;) – user2504380 2014-10-09 08:44:14

+0

@Tom謝謝。現在我想初始化字符串[] countryNames爲252.你會怎麼做 – user2738145 2014-10-09 09:01:06

回答

6

此行while((input=scan.nextLine())!=null)嘗試直到input變爲null。但如果沒有更多行可用,則nextLine()將引發異常。

爲了避免這種情況,你的代碼更改爲以下

//String input=""; // this line is obsolete 
scan = new Scanner(file); 
scan.nextLine(); 
while(scan.hasNextLine()) { 
    final String input = scan.nextLine(); // read the line after the check (to make sure there is a line available) 
    String[] countryNames = input.split(","); 
    //int a = Integer.parseInt(countryNames[1]); 
    System.out.println(countryNames[0]); 
    //System.out.println(a); 
} 
0

我覺得countryNames數組爲null。嘗試在添加數據之前初始化它。 調試代碼,並檢查countryNames ...

String[] countryNames = new String[...] 

,如果你不知道數組長度,你最好使用ArrayList。 (使用ArrayList的時候,你不必先初始化。)

2

也許這樣,你有一個特定的方法來測試是否掃描有更多的元素來讀取

String input=""; 
scan = new Scanner(file); 
scan.nextLine(); 
while(scan.hasNext()) 
{ 
    input=scan.nextLine(); 
    String[] countryNames = input.split(","); 
    //int a = Integer.parseInt(countryNames[1]); 
    System.out.println(countryNames[0]); 
    //System.out.println(a); 
} 
scan.close(); 
4

的問題在線程提出已經解決了

while(scan.hasNextLine()) { 

但是,還有,你會在你的代碼中找到了另一個問題。在循環內部,你已經創建了一個局部變量countryNames [],它與類中聲明的String [] countryNames無關。

我建議你更改宣言書就是這樣:

public class CSVReader {  
    List<String> countryNames; 

而且在類的構造函數初始化:

public CSVReader(String filename)// throws FileNotFoundException 
{ 
    countryNames = new ArrayList<String>(); 
    setUp(filename); 
} 

的ArrayList允許你動態添加值。因此,在循環中,您必須將每個國家/地區的名稱添加到此ArrayList中:

String[] countryValues = input.split(","); 
countryNames.add(countryValues[0]);