在java中读取文件时出错
需要帮助修复此错误。我正在尝试打印字符串CountryName的数组长度在java中读取文件时出错,java,Java,需要帮助修复此错误。我正在尝试打印字符串CountryName的数组长度 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 Tes
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)
下面是我的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个国家的数据存储在一个字符串数组中,年份数据存储在一个整数数组中,每个国家的手机数据统计数据存储在一个二维双数组中。我是编程新手,我想知道我的方向是否正确,如果没有,你会如何在字符串数组中命名国家,在整数中命名年份,在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
我认为countryNames数组为空。在添加数据之前尝试初始化它。 调试您的代码并检查CountryName
String[] countryNames = new String[...]
如果不知道数组的长度,最好使用Arraylist。
(当使用Arraylist时,您不必首先初始化。)也许像这样,您有一个特定的方法来测试扫描是否有更多的元素要读取
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();
此行同时((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);
}
线程中提出的问题已通过
while(scan.hasNextLine()) {
但在代码中还存在另一个问题。在循环中,您创建了一个局部变量countryNames[],它与类中声明的字符串[]countryNames不相关
我建议您更改声明,如下所示:
public class CSVReader {
List<String> countryNames;
在你阅读之前,你可以先检查是否还有一行。因此
while(scanner.hasNextLine())
而不是while((input=scan.nextLine())!=null)
可以帮助您。这是答案,所以只需将其作为答案书写;)@汤姆,谢谢。现在我想将字符串[]countryNames初始化为252。你打算怎么办that@user2738145行String[]countryNames=input.split(“,”)代码>创建一个在循环体中有效的新变量countryNames
。每次迭代后,该变量将被删除/覆盖。我建议使用列表
并使用countryNames.add(data[0])
行和String[]data=input.split(“,”)代码>和声明为<代码>列表countryNames=new ArrayList()的countryNames代码>作为一个类字段。@Tom oh好的,但要求不要使用列表,因为教授还没有教过。刚刚完成阵列,需要使用1d阵列存储国家,1d阵列存储年份,2d阵列存储国家及其统计数据
public class CSVReader {
List<String> countryNames;
public CSVReader(String filename)// throws FileNotFoundException
{
countryNames = new ArrayList<String>();
setUp(filename);
}
String[] countryValues = input.split(",");
countryNames.add(countryValues[0]);