Screen Image I began to make an airline reservation system where Planes.out is a list of plane variables and moderators.in is different moderator passwords. When I run the code I get the exception in the title:
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at Main.initializePlanes(Main.java:59)
at Main.main(Main.java:83)
exit status 1
I've never heard of this and I'm not sure how to fix it, here is my code:
import java.util.*;
import java.io.*;
class Main {
public static void planeMaker(ArrayList<Plane> planes) throws IOException {
boolean tvs;
Scanner input = new Scanner(System.in);
System.out.println("PLANE MAKER\n\n");
System.out.println("\nEnter the name:\t");
String name = input.nextLine();
System.out.println("\nEnter the destination:\t");
String destination = input.nextLine();
System.out.println("\nEnter the depart location:\t");
String departLocation = input.nextLine();
System.out.println("\nEnter the amount of seats:\t");
int seats = input.nextInt();
System.out.println("\nEnter the amount of rows:\t");
int rows = input.nextInt();
System.out.println("\nEnter the amount of columns:\t");
int columns = input.nextInt();
System.out.println("\nAre there tvs?:\t");
String buffer = input.nextLine();
String qr = input.nextLine();
if(qr.equalsIgnoreCase("yes")) {
tvs = true;
} else {
tvs = false;
}
System.out.println("\nEnter the seat cost:\t");
int seatCost = input.nextInt();
System.out.println("\nEnter the boarding time (7:30 -> 730):\t");
int boardingTime = input.nextInt();
System.out.println("\nEnter the flight duration (in mins):\t");
int flightDuration = input.nextInt();
System.out.println("\nEnter the day of the month of takeoff:\t");
int dayOfTheMonth = input.nextInt();
FileWriter fw = new FileWriter("Planes.out");
PrintWriter output = new PrintWriter(fw);
output.print(name + " " + destination + " " + departLocation + " " + seats + " " + rows + " " + columns + " "+ tvs+ " "+ seatCost+" "+boardingTime+" "+flightDuration+" "+dayOfTheMonth);
}
public static void modIn(ArrayList<Plane> planes) throws IOException {
Scanner input = new Scanner(System.in);
System.out.println("What would you like to do?\n");
System.out.println("\n\t1.) Create a Plane\n\t2.) Change a flight time");
int qeu = input.nextInt();
if(qeu == 1) {
planeMaker(planes);
} else if(qeu == 2){
changeBoardingTime(planes);
}
}
public static ArrayList<Plane> initializePlanes() {
Scanner initialize = new Scanner("Planes.out");
String name, destination, departLocation;
int seats, rows, columns, seatCost, boardingTime, flightDuration, dayOfTheMonth;
boolean tvs;
ArrayList<Plane> plan = new ArrayList<Plane>();
while(initialize.hasNext()) {
name = initialize.next();
destination = initialize.next();
departLocation = initialize.next();
seats = initialize.nextInt();
rows = initialize.nextInt();
columns = initialize.nextInt();
tvs = initialize.nextBoolean();
seatCost = initialize.nextInt();
boardingTime = initialize.nextInt();
flightDuration = initialize.nextInt();
dayOfTheMonth = initialize.nextInt();
plan.add(new Plane(name,destination,departLocation,seats,rows,columns,tvs,seatCost, boardingTime, flightDuration, dayOfTheMonth));
}
initialize.close();
return plan;
}
public static void changeBoardingTime(ArrayList<Plane> planes) {
System.out.println("What plane would you like to change the boarding time of?");
System.out.println("PLANES:");
for(int i = 0; i < planes.size(); i++) {
System.out.println("\t"+planes.get(i).getName());
}
}
public static void main(String[] args) throws IOException{
ArrayList<Plane> planes = new ArrayList<Plane>();
planes = initializePlanes();
boolean qr = false;
FileWriter fw = new FileWriter("Output1.out");
PrintWriter output = new PrintWriter(fw);
Scanner input = new Scanner(System.in);
System.out.println("Hello, welcome to Airway Airline reservation system.\nPlease enter what you would like to do next: ");
System.out.println("\t1.) Book a seat\n\t2.) View your reservation\n\t3.) Sign into moderator");
int question = input.nextInt();
if(question == 1) {
} else if(question == 2) {
} else if(question == 3) {
Scanner modPass = new Scanner(new File("Moderators.in"));
System.out.println("\nPlease enter your moderator password: ");
String buffer = input.nextLine();
String modAsk = input.nextLine();
while(modPass.hasNext()) {
if(modPass.next().equals(modAsk)) {
modIn(planes);
qr = true;
}
}
if(qr == false) {
System.out.println("YOU ARE NOT A MODERATOR!!!");
System.exit(0);
}
}
}
}
I've researched and checked other articles but I can't find a solution, I've also tried closing all the different filewriters, to no luck.