import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class CSVReader
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner scanner = new Scanner(new File("Lunch.csv"));
ArrayList<String> AccountNum = new ArrayList<String>();
ArrayList<String> AccountBal = new ArrayList<String>();
scanner.useDelimiter(",");
while(scanner.hasNext())
{
AccountNum.add(scanner.next());
AccountBal.add(scanner.next());
}
scanner.close();
display(AccountNum, AccountBal);
}
public static void display(ArrayList AccountNum, ArrayList AccountBal)
{
System.out.println("\nThe size of the list is " + AccountNum.size());
for(int x = 0; x < AccountNum.size(); ++x)
{
System.out.println("position " + x + " Number: " + AccountNum.get(x));
System.out.println("position " + x + " Number: " + AccountBal.get(x));
}
}
}
该程序应该从 CSV 文件中读取到两个数组中。它编译得很好,但是当我运行它时它会抛出 NoSuchElementException。该程序仅在使用一个数组时有效。有谁知道它为什么会抛出这个异常以及如何解决它?