0

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.

  • I checked the file and there are 11 different values seperated by spaces: Boeing Boston Portland 40 10 4 true 49 500 100 15. There are 11 calls for data and all the types match up. – Shane Woloszyn Mar 18 '23 at 00:44
  • OK, found the problem - check the [documentation](https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/util/Scanner.html#%3Cinit%3E(java.lang.String)}) of the constructor used to create the `Scanner`: "*Constructs a new Scanner that produces values scanned from the specified string.*" - it is reading `name` as `"Planes.out"` and nothing else left for `destination` – user16320675 Mar 18 '23 at 00:51
  • I'm not sure what you mean, I tried to create a new file out of the scanner and I'd just input data manually into it but it didn't work, returned filenotfoundexception. – Shane Woloszyn Mar 18 '23 at 00:59
  • If `Panes.out` is a _file_, then use `new Scanner(new File("Planes.out"))`. That will scan a file named `Planes.out` in the working directory. Obviously, that file has to exist in that location for it to work. – Slaw Mar 18 '23 at 01:02
  • Using new Scanner(new File("Planes.txt")); returns FileNotFoundException, despite having a file named Planes.txt with the data inside – Shane Woloszyn Mar 18 '23 at 01:06
  • And that text file is located in the current working directory? – Slaw Mar 18 '23 at 01:07
  • Yes, I'll add a screenshot to the post of my screen with the directory on the left – Shane Woloszyn Mar 18 '23 at 01:07
  • Ah, that is not a run-time error as I assumed. You're getting a compiler error. `FileNotFoundException` is a checked exception, and thus must be explicitly handled. See https://stackoverflow.com/questions/19788989/error-unreported-exception-filenotfoundexception-must-be-caught-or-declared-to – Slaw Mar 18 '23 at 01:11
  • /home/runner/Airline-Reservation-System/Planes.txt is printed from new File("Planes.txt").getAbsolutePath() – Shane Woloszyn Mar 18 '23 at 01:11
  • The duplicate of the Q&A I linked is more comprehensive: https://stackoverflow.com/questions/72751842/what-does-error-unreported-exception-xxx-must-be-caught-or-declared-to-be-t – Slaw Mar 18 '23 at 01:13
  • I tried to use throws FileNotFoundException earlier but it just spat out more errors, Whatever I changed in the time between then and now made it work, thanks. – Shane Woloszyn Mar 18 '23 at 01:14
  • .........plan.add(new Plane.................. Why are you calling an instance inside a static context !!!!! -w Also, you do not store any variable information globally as you aquire it. – Samuel Marchant Mar 18 '23 at 03:16
  • @SamuelMarchant The OP declared and initialized `plan` inside the method (i.e., it's local to the method). And calling a constructor inside a static method is perfectly legal. There's no problem there. Also, the method returns the list (i.e., `plan`), allowing it to be used elsewhere (e.g., the OP passes the list to `modIn`). So, there's no reason to "store any variable information globally". – Slaw Mar 18 '23 at 16:17
  • Then it probably means it either oversteps for another next() and there is no way to break and or safely count the inputs. – Samuel Marchant Mar 21 '23 at 16:46

0 Answers0