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();
}
}