Java I / O程序,java.util.NoSuchElementException

时间:2015-11-13 23:26:07

标签: java io nosuchelementexception

我正在开发一个程序,该程序从文本文件中读取输入并将其输出到多个.csv文件中。我遇到了我的驱动程序类和inputFile类的问题。当我运行程序时,我收到此错误

input error

这是我的驱动程序代码:

import java.util.Scanner;

public class AirlineDriver
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        System.out.print("Enter name of file: ");
        Scanner keyboard = new Scanner(System.in);
        String filename = keyboard.nextLine();

        AirlineInput input = new AirlineInput();

        input.openFile(filename);

        Airline airline1 = input.readFile();
        Airline airline2 = input.readFile();
        Airline airline3 = input.readFile();
        Airline airline4 = input.readFile();
        Airline airline5 = input.readFile();
        Airline airline6 = input.readFile();
        Airline airline7 = input.readFile();
        Airline airline8 = input.readFile();    

        input.closeFile();

        AirlineOutput output = new AirlineOutput();

        output.openFile(airline1);
        output.openFile(airline2);
        output.openFile(airline3);
        output.openFile(airline4);
        output.openFile(airline5);
        output.openFile(airline6);
        output.openFile(airline7);
        output.openFile(airline8);

        output.closeFile();
    }
}

这是我输入类的代码

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class AirlineInput
{
    Scanner input = null;

    public Scanner openFile(String filename)
    {

        try
        {
            input = new Scanner(new File(filename));
        }
        catch (FileNotFoundException e)
        {
            System.out.println("Unable to open file" + filename);
            System.exit(0);
        }
        return input;
    }

    public Airline readFile()
    {

        String airline = input.nextLine();
        String iataDesignator = input.nextLine();
        String icaoDesignator = input.nextLine();
        String callSign = input.nextLine();
        String frequentFlyer = input.nextLine();
        String alliance = input.nextLine();
        String city = input.nextLine();
        String state = input.nextLine();
        String country = input.nextLine();
        String website = input.nextLine();

        Designator designate = new Designator(iataDesignator, icaoDesignator, callSign);
        Location location = new Location(city, state, country);
        Airline airlineObject = new Airline(airline, designate, frequentFlyer, alliance, location, website);

        return airlineObject;
    }

    public void closeFile()
    {
        input.close();
    }
}

和我用于输入的文本文件

Alaska Airlines
IATA ASA ALASKA
Mileage Plan

SeaTac
WA
USA
alaskaair.com
Allegiant Air
G4 AAY ALLEGIANT


Enterprise
NV
USA
allegiantair.com
Air Canada
AC ACA AIR_CANADA
Aeroplan
Star Alliance
Montreal
QC
Canada
aircanada.com
Lufthansa
LH DLH LUFTHANSA
Miles & More
Star Alliance
Cologne

Germany
lufthansa.com
American Airlines
AA AAL AMERICAN
AAdvantage
Oneworld
Fort Worth
TX
USA
aa.com
United Airlines
UA UCAL UNITED
MileagePlus
Star Alliance
Chicago
IL
USA
united.com
Delta Airlines
DL DAL DELTA
SkyMiles
SkyTeam
Atlanta
GA
USA
delta.com
Hawaiian Airlines
HA HAL HAWAIIAN
HawaiianMiles

Honolulu
HI
USA
hawaiianair.com

1 个答案:

答案 0 :(得分:1)

您的readFile()方法读取每个航空公司的 10 行,但该文件每个航空公司只包含 8 行。

10 reads          8 lines

airline           Alaska Airlines
iataDesignator    IATA ASA ALASKA
icaoDesignator    Mileage Plan
callSign          
frequentFlyer     SeaTac
alliance          WA
city              USA
state             alaskaair.com
country           
website           


10 reads          8 lines

airline           Allegiant Air
iataDesignator    G4 AAY ALLEGIANT
icaoDesignator    
callSign          
frequentFlyer     Enterprise
alliance          NV
city              USA
state             allegiantair.com
country           
website