Wtalk2
Wtalk2

Reputation: 43

(Beginner) Java program terminating for no reason

I am currently working on a hotel reservation practice program (I'm new to Java so I'm practicing by making random programs) For the life of me I can't figure out why the program terminates after I enter my number of guests, I wrote the program in Eclipse and I am compiling the program in Eclipse also. Any help would be appreciated.

My code:

package text;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import org.apache.commons.lang3.ArrayUtils;

public class HotelReservations {
    public static void main(String[] args){

        @SuppressWarnings("resource")
        Scanner in = new Scanner(System.in);

        int Guests;
        String Reserved;

        int[] RoomsArray = new int[10];

        RoomsArray[0] = (int) ((10 * Math.random()) - 1);
        RoomsArray[1] = (int) ((10 * Math.random()) - 1);
        RoomsArray[2] = (int) ((10 * Math.random()) - 1);
        RoomsArray[3] = (int) ((10 * Math.random()) - 1);
        RoomsArray[4] = (int) ((10 * Math.random()) - 1);
        RoomsArray[5] = (int) ((10 * Math.random()) - 1);
        RoomsArray[6] = (int) ((10 * Math.random()) - 1);
        RoomsArray[7] = (int) ((10 * Math.random()) - 1);
        RoomsArray[8] = (int) ((10 * Math.random()) - 1);
        RoomsArray[9] = (int) ((10 * Math.random()) - 1);   

        Date Date = new Date( );
            SimpleDateFormat ft = 
            new SimpleDateFormat ("EEE, d MMM yyyy 'at' hh:mm a!");

        System.out.println("Today's date is: " + ft.format(Date));

        System.out.println();

        System.out.println("Enter the number of guests:");
        Guests = in.nextInt();

        if (Guests > 8)
        {
            System.out.println("The maximum amount of guests allowed per room is 8. Please split groups of more than 8 into seperate rooms.");
            System.exit(0);
        } 
        else if (ArrayUtils.contains(RoomsArray, 0))
        {
            System.out.println("A room is available! The closest available room is: Room " + ArrayUtils.indexOf(RoomsArray, 0));
        } 
        else 
        { 
            System.out.println("Sorry, no rooms are available.");
        }

        System.out.println("Would you like to reserve this room? (Yes/No)");
        Reserved = in.nextLine();

        if (Reserved.equals("Yes")) //here is where i changed it, now it will check yes/no
        {
            System.out.println("Hi");
        } else 
        {
            System.out.println("No");
        }
    }   
}

Upvotes: 1

Views: 759

Answers (1)

Ravi Teja N
Ravi Teja N

Reputation: 145

I guess you are trying to use same scanner--in for reading the input for integers and text

It is not recommended to use same scanner for reading integers and reading text input

use different scanners like scan1 and scan2
scan1 for integers
scan2 for next line as string

Scanner scan1= new Scanner(System.in);
System.out.println(q.nextInt());
Scanner scan2= new Scanner(System.in);
System.out.println(scan2.nextLine());

Upvotes: 2

Related Questions