java.util.NoSuchElementException只使用一个Scanner

时间:2016-04-20 14:05:17

标签: java csv nosuchelementexception

所以,我正在尝试使用Scanner读取.csv文件的内容,每当我收到标题所说的错误时。

首先,我在Internet上读到这是因为你的项目中不能有多个Scanner实例,因为它们可以使用输入,所以我用BufferedReader来扫描用户选择和扫描程序以打开.csv文件。

现在我再次收到错误,只有一个扫描程序实例,我不知道为什么会这样。

Question.java

import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Question {
    public static void menu() {
        System.out.println("\nEnter your choice :\n");
        System.out.println("1 - Load Hotels and Reservations from file");
        System.out.println("\nEnter \"1\"");
        int choice = 1;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
            choice = Integer.parseInt(br.readLine());
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        if (choice == 1)
            loadOption();
    }

    public static void loadOption() {
        Scanner scan;
        ArrayList<Hotel> hotel = new ArrayList();
        try {
            Hotel hotel = new Hotel();
            scan = new Scanner(new File("data1.csv"));
            scan.useDelimiter(";");
            int column = 1;
            while (scan.hasNext()) {
                scan.next();
                if (column == 1)
                    hotel.id = scan.next();
                if (column == 2)
                    hotel.name = scan.next();
                if (column == 3)
                    hotel.star = scan.next();
                if (column == 4)
                    hotel.rooms = scan.next();
            }
            System.out.print("Files Succesfully Loaded!!!\n");
            scan.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Question obj = new Question();
        obj.menu();
    }

};

Hotel.java

public class Hotel extends Question {
    String id, star, rooms, name;

    public Hotel() {
    } // constructor
};

这是我编译它们的方式:

javac Hotel.java
javac Question.java
java Question

确切的错误是这样的:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1371)
    at Question.loadOption(Question.java:43)
    at Question.menu(Question.java:27)
    at Question.main(Question.java:62)

2 个答案:

答案 0 :(得分:4)

您检查hasNext(),然后拨打next()两次。所以第二个电话是未经检查的。更糟糕的是,第一次通话不是必需的,也没有效果。您也不会增加column变量。

试试这个:

Hotel hotel = new Hotel();
scan = new Scanner(new File("data1.csv"));
scan.useDelimiter(";");
int column=1; 
while (scan.hasNext()) {
  //scan.next(); this is not needed
  if (column == 1)
    hotel.id = scan.next();
  if (column == 2)
    hotel.name = scan.next();
  if (column == 3)
    hotel.star = scan.next();
  if (column == 4)
    hotel.rooms = scan.next();
  //this is needed as well:
  column++;
}
System.out.print("Files Succesfully Loaded!!!\n");
scan.close();

注意:您仍然只创建一个酒店实例。

答案 1 :(得分:1)

 String csvFile = "data1.csv";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";
     ArrayList <Hotel> hotel = new ArrayList();
    BufferedReader br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {
            Hotel hotelob = new Hotel();
            // use comma as separator
            String[] hotelproperties = line.split(cvsSplitBy);
            //setting properties to hotelobject
            hotelob.id = hotelproperties[0];  
            hotelob.name =hotelproperties[1];
            hotelob.star =hotelproperties[2];
            hotelob.rooms =hotelproperties[3];
            //adding the object to arraylist 
            hotel.add(hotelob);  
        }

如果您使用bufferedreader这样可以轻松地执行此操作,您可以处理多行,同时您可以轻松地split来自每行的数据。将此代码放入您的loadoption方法中,这可能有所帮助。