-1

here is the code:

import java.util.Scanner;

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

    int roomNumber;
    int numberOfRooms;
    int optionNumber;
    String guestName = null;
    String phoneNumber;
    int nightsStaying;
    double nightlyRate;

    Scanner keyboard = new Scanner(System.in);
    System.out.println("How many rooms are in the hotel?");
    numberOfRooms = keyboard.nextInt();
    Hotel Carnegie = new Hotel(numberOfRooms);
    do
    {
        System.out.println("Welcome to the MENU!");
        System.out.println("Please enter a number corresponding to the option desired."); // main menu screen
        System.out.println("OPTION 1: Put guest into room");
        System.out.println("OPTION 2: Remove guest from room");
        System.out.println("OPTION 3: Get guest info");
        System.out.println("OPTION 4: Get guest count");
        System.out.println("OPTION 5: Display guest book");
        System.out.println("OPTION 6: Calculate expected income");
        System.out.println("OPTION 7: Quit");

        optionNumber = keyboard.nextInt(); // choosing an option
        if (optionNumber == 1) // if option 1 is chosen
        {

            System.out.println("What is the guests first name?");
            guestName = keyboard.nextLine();
            System.out.println("What is the guests Phone number?");
            phoneNumber = keyboard.nextLine();
            Guest newGuest = new Guest(guestName);
            newGuest.setPhoneNumber(phoneNumber);

"What is the guests first name? What is the guests Phone number?" appears and it will not take anything for the guest name. guestName has been initialized to "". lines 36 -39. not sure of the issue going on. i am using a few methods contained within my hotel class, however i am almost certain they do not contain the error which causes this bug. also the do loop is completed later on in the code.

Will
  • 11
  • 4

2 Answers2

0
keyboard.nextLine();
guestName = keyboard.nextLine();

Skip a line by adding keyboard.nextLine(); before reading guestname. This works for me.

Siguza
  • 21,155
  • 6
  • 52
  • 89
Mohan Raj
  • 1,104
  • 9
  • 17
-2

change the code as shown below then it will work

import java.util.Scanner;

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

    int roomNumber;
    int numberOfRooms;
    int optionNumber;
    String guestName = null;
    String phoneNumber;
    int nightsStaying;
    double nightlyRate;

    Scanner keyboard = new Scanner(System.in);
    System.out.println("How many rooms are in the hotel?");
    numberOfRooms = Integer.parseInt(keyboard.nextLine());
    Hotel Carnegie = new Hotel(numberOfRooms);
    do
    {
        System.out.println("Welcome to the MENU!");
        System.out.println("Please enter a number corresponding to the option desired."); // main menu screen
        System.out.println("OPTION 1: Put guest into room");
        System.out.println("OPTION 2: Remove guest from room");
        System.out.println("OPTION 3: Get guest info");
        System.out.println("OPTION 4: Get guest count");
        System.out.println("OPTION 5: Display guest book");
        System.out.println("OPTION 6: Calculate expected income");
        System.out.println("OPTION 7: Quit");

        optionNumber = Integer.parseInt(keyboard.nextLine());; // choosing an option
        if (optionNumber == 1) // if option 1 is chosen
        {

            System.out.println("What is the guests first name?");
            guestName = keyboard.nextLine();
            System.out.println("What is the guests Phone number?");
            phoneNumber = keyboard.nextLine();
            Guest newGuest = new Guest(guestName);
            newGuest.setPhoneNumber(phoneNumber);
Fathah Rehman P
  • 8,401
  • 4
  • 40
  • 42