I am working on a reservation app in Java and it is almost complete but I keep getting this error when running my app:

Exception in thread "main" java.util.NoSuchElementException: No line 
found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at ReservationCalculator.main(ReservationCalculator.java:34)

Full Code

    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.time.format.FormatStyle;
     import java.util.Scanner;

public class ReservationCalculator {

double perNight = 115.00, totalPrice;
long noOfDays;
int m, d, y;
LocalDate arrivalDate, departureDate;

public static void main(String[] args) {

    System.out.println("Welcome to the Reservation Calculator");
    System.out.println();
    Scanner sc = new Scanner(System.in);
    ReservationCalculator rc = new ReservationCalculator();     

    String choice = "y";

    while(choice.equalsIgnoreCase("y"))
    {
        rc.accept();

        System.out.println(rc.getArrivalDateFormatted());

        System.out.println(rc.getDepartureDateFormatted());

        System.out.println(rc.getPricePerNightFormatted());

        System.out.println(rc.getTotalPriceFormatted());

        choice = sc.nextLine();
        System.out.println("Would you like to continue? (y/n)");

        System.out.println();
    }
    sc.close();
   } //End MAIN

public void setArrivalDate(LocalDate arrivalDate) {

    this.arrivalDate = arrivalDate;
}

void setDepartureDate(LocalDate departureDate) {

    this.departureDate = departureDate;
}

LocalDate getArrivalDate() {

    return arrivalDate;
}

LocalDate getDepartureDate() {

    return departureDate;
}

String getArrivalDateFormatted() {

    String arrivalDateFormat;

    arrivalDateFormat = "Arrival Date: " + arrivalDate.getMonth() + 
          " " + String.valueOf(arrivalDate.getDayOfMonth()) + ", "

    + String.valueOf(arrivalDate.getYear());

    return arrivalDateFormat;

}

String getDepartureDateFormatted() {

    String arrivalDateFormat;

    arrivalDateFormat = "Departure Date: " + 
       departureDate.getMonth() + " " + 
        String.valueOf(departureDate.getDayOfMonth()) + ", "

    + String.valueOf(departureDate.getYear());

    return arrivalDateFormat;
}

    String getPricePerNightFormatted() {

    String ppn = "Price: $" + String.valueOf(perNight) + " per night";

    return ppn;
}

long getNumberOfNights() {

    noOfDays = departureDate.toEpochDay() - 
          arrivalDate.toEpochDay();

    return noOfDays;

}

double getTotalPrice() {

    totalPrice = (double) getNumberOfNights() * perNight;

    return totalPrice;

}

String getTotalPriceFormatted() {

    String finA = "Total price: $" + String.valueOf(getTotalPrice()) + "                 
            for " + String.valueOf(getNumberOfNights())

    + " nights";

    return finA;

        }

public void accept() {

    LocalDate arrivalDate1, departureDate1;

    DateTimeFormatter fmt =         
    DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);

    Scanner sc = new Scanner(System.in);

    System.out.print("Enter the arrival month (1-12):");

    m = sc.nextInt(); // Accept data in integer format

    System.out.print("Enter the arrival day (1-31):");

    d = sc.nextInt();

    System.out.print("Enter the arrival year:");

    y = sc.nextInt();

    arrivalDate1 = LocalDate.of(y, m, d);

    fmt.format(arrivalDate1);

    setArrivalDate(arrivalDate1);

    System.out.print("Enter the departure month (1-12):");

    m = sc.nextInt(); // Accept data in integer format

    System.out.print("Enter the departure day (1-31):");

    d = sc.nextInt();

    System.out.print("Enter the departure year:");

    y = sc.nextInt();

    departureDate1 = LocalDate.of(y, m, d);

    fmt.format(arrivalDate1);

    setDepartureDate(departureDate1);

    //Close scanner object
    sc.close();
}   
}

You have 2 Scanners both based on System.in
At the end of the accept method you close one of them
Closing a Scanner also closes the underlying stream (see API doc), which is System.in
The you try to read from the other Scanner, but its underlying stream has been closed
... so you get the Exception.

In general you should never have 2 Scanners using the same input stream.

Other notes:
If you have a loop that will always be executed at least once, use a do...while, not a while, the code is much simpler and safer (see my previous post)

Advance warning:
If you mix nextInt() and nextLine() in a Scanner you will fall foul of a trap that the designers stupidly built into Scanner.
See here for explanation.

JC

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.