0

I am trying to make a small application which displays a menu to the user with selections. So far I have this:

 package hotel;
 import java.util.*;

 public class Hotel {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    Room[] myHotel = new Room[10];
    myHotel[0] = new Room ();
    myHotel[1] = new Room ();
    myHotel[2] = new Room ();
    myHotel[3] = new Room ();
    myHotel[4] = new Room ();
    myHotel[5] = new Room ();
    myHotel[6] = new Room ();
    myHotel[7] = new Room ();
    myHotel[8] = new Room ();
    myHotel[9] = new Room ();

String roomName;
String menuEntry;
int roomNum = 0;
String[] hotel = new String[11]; 
for (int x = 0; x < 6; x++ ) hotel[x] = "";
initialise(hotel);


System.out.println("Enter one of the following:\nA) Add\nD)\nE)\nF)\nL)\nO)\nS)\nV) View all rooms\nInput:");
menuEntry = input.next();

if (menuEntry == "A")
{
while ( roomNum < 11 )
{
    for (int x = 0; x < 10; x++ )
    {
        if (hotel[x].equals("e"))System.out.println("room " + x + " is empty");
        }
            System.out.println("Enter room number (0-9) or 10 to stop:" );
            roomNum = input.nextInt();
            System.out.println("Enter name for room " + roomNum +" :" ) ;
            roomName = input.next();
            hotel[roomNum] = roomName ;
}
}


if (menuEntry == "V")
{
for (int x = 0; x < 10; x++ )
{
System.out.println("room " + x + " occupied by " + hotel[x]);
}
}

}   
private static void initialise( String hotelRef[] ) {
for (int x = 0; x < 10; x++ ) hotelRef[x] = "empty";
System.out.println( "initilise\n");
}
}

The menu is displayed but when an entry is made the loop is ended and "Built successful" is displayed.

Any help appreciated :)

Oscar
  • 511
  • 2
  • 10
  • 25
  • 1
    use `menuEntry.equals("A")` instead of `==`. – Atri Mar 01 '16 at 18:11
  • @Atri When you compare String reference with String literal no need to use `equals` it will work with `==` , so your comment is wrong – Pragnani Mar 01 '16 at 18:18
  • 1
    @Pragnani That's not correct. If the String is already there in the pool then it might return `true` otherwise if you are running the program for the first time then it will always return `false`. – user2004685 Mar 01 '16 at 18:23
  • @PragnaniKinnera `new String("test") == "test" // --> false` – Atri Mar 01 '16 at 18:29
  • @user2004685 yes I know, you are correct I should have mentioned that in comment – Pragnani Mar 01 '16 at 18:29
  • @Atri I guess I need to give more explanation regarding the same, I have posted that comment considering String object created without using new, to make both of us right, call intern on new String object. – Pragnani Mar 01 '16 at 18:34
  • @PragnaniKinnera does `Scanner.next()` create a new `String` object? I think it does, because when I run this program `menuEntry == "A"` is `false`. – Atri Mar 01 '16 at 18:50

0 Answers0