java.util.NoSuchElementException 仅使用一台扫描仪

java.util.NoSuchElementException using only one 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)

您检查 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();

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

 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 方法中,这可能会有所帮助。