1

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");
        }
    }   
}
Wtalk2
  • 43
  • 5
  • 2
    It will print something and then immediately terminate, because *thats how the program is written*. – flotothemoon Aug 10 '14 at 18:20
  • @1337 Can you explain why this is happening? I'm really new to Java. – Wtalk2 Aug 10 '14 at 18:22
  • Have a look here http://stackoverflow.com/questions/7416018/when-does-the-main-thread-stop-in-java If you still can't figure, I can elaborate here – shlomi33 Aug 10 '14 at 18:23
  • Because there is no more code after printing the text. I guess you want to put in in a loop. – flotothemoon Aug 10 '14 at 18:23
  • It will print something and then immediately terminate does it print "Would you like to reserve this room? (Yes/No)") – Madhawa Priyashantha Aug 10 '14 at 18:23
  • 1
    Spend some time reading the free-of-cost [online Tutorial](http://docs.oracle.com/javase/tutorial/index.html) provided by Oracle. In this case, the pages on [`while` loops](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html) and [`for` loops](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html) and [scanning](http://docs.oracle.com/javase/tutorial/essential/io/scanning.html). – Basil Bourque Aug 10 '14 at 18:48

1 Answers1

2

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());
Ravi Teja N
  • 145
  • 2
  • 12