-1

Basic Programming Concepts Lab # 9 – Arrays Write a cinema reservation program to allow customer to reserve cinema seating. At the beginning of the program, ask the customer to input the name and display customize greeting message. Once the seat is reserved, it should be marked as ‘**’. If customer chooses a seat that has been reserved by other customer, display a message to inform customer and suggest the next available seat. Sample output:

Hi, what is your name? Amy

Welcome, Amy!


CINEMA 1 SEATING PLAN


00 01 02 03 04
05 06 07 08 09
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
25 26 27 28 29
Which seat do you want? 21

Would you like to make another booking?(Y or N)

If seat chosen has been reserved by others, advice next available seat.

Now for the code. The problem with the code is because I probably don't know how to change someone's "y" into a "true" for boolean. Why can't coding ever be easy?

import java.util.*;

public class cinema
{

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

    int booking;
    String name;
    int suggestion = 0;
    boolean check1 = true;
    String check2 = "_";
    String check11 = "_";

    //create array
    final int ARRAY_LENGTH = 30;
    int[] array = new int[ARRAY_LENGTH];

    String[] seats = { "00", "01" , "02" , "03" , "04" , "05" , "06" , "07" , "08" , "09" , "10"
                         , "11" , "12" , "13" , "14" , "15" , "16" , "17" , "18" , "19" , "20" , "21"
                          , "22" , "23" , "24" , "25" , "26" , "27" , "28" , "29"};

    System.out.println("Hi, what is your name?");
    name = input.nextLine();

    System.out.printf("Welcome,%s!\n",name);

    System.out.println("*********************");
    System.out.println("CINEMA 1 SEATING PLAN");
    System.out.println("*********************");

    do{

    for(int a = 0; a < 30; a++ ){
    System.out.printf("%s\t", seats[a]);
    if (a % 5 == 4){
    System.out.printf("\n");
    }
    }
    System.out.println("Which seat do you want?");
    booking = input.nextInt();

    if(seats[booking] == "**"){
        System.out.println("Sorry! Seat taken.");
        for(int b = 0; b < 30; b++){
            if(seats[booking + b] == "**"){
                check2 = seats[booking + b];
            }
            else if(seats[booking - b] == "**"){
                check2 = seats[booking - b];
            }
            else{
            }
        }//end for loop
        System.out.printf("Suggest to take seat:%s\n", check2);
    }//end if

    seats[booking] = "**";

    System.out.println("Would you like to make another booking?(Y or N)");
    check11 = input.nextLine();

    while(check11 == "y"||check11 == "n"){
    if(check11 == "y"){
        check1 = true;
        System.out.print("a");
    }
    else if(check11 == "n"){
        check1 = false;
        System.out.print("b");
    }
    else{
    System.out.println("Would you like to make another booking?(Y or N)");
    check11 = input.nextLine();
    }
    }//end while


    }while(check1);//outer while


}
//end main
}

Please don't flag it as a duplicate. The problem in it is different. I can't get the

"Would you like to make another booking?(Y or N)"

to work. When I meant I couldn't get it to work, I meant that the program wouldn't let me have the opportunity to enter y or n. The true problem is the check11 = input.nextLine();. Anyone can tell me what's wrong with it?

Also, the previous reference really didn't help. I didn't understand how it refers to my quetion.

Gerald Loo
  • 21
  • 7
  • Aside from the linked question, which answers *"I probably don't know how to change someone's "y" into a "true" for boolean"*, also note that `while (check1 == true)` is a bad idea; just `while (check1)` is all you need (probably with a more expressive variable name, such as `keepLooping`). `check1` is *already* a boolean, so there's no need to use `==` to get another boolean. Always use `if (flag)` or `if (!flag)`. – T.J. Crowder Nov 20 '16 at 10:16
  • 1
    *"Also, the previous reference really didn't help. I didn't understand how it refers to my quetion."* Then read it more carefully. You said you didn't know how to turn someone's `"y"` into a boolean. That question's answers tell you how to turn `"y"` into a boolean. If you meant to ask something else, then you need to be clear about that with a much more targeted question, ideally with a [mcve]. – T.J. Crowder Nov 20 '16 at 10:27
  • I don't know how to do minimal. But I'm pretty sure I got complete and verifiable. Complete as in that is the exact copy of the code I'm trying to fix. Verifiable as in I tried it repeatedly on my PC and on internet software. The "Would you like to make another booking?(Y or N)" creates an eternal loop that is luckily stopped by System.out.println("Which seat do you want?"); booking=input.nextInt(); Please forgive this novice, beginner, college, diploma student. – Gerald Loo Nov 20 '16 at 10:36

1 Answers1

0

To compare two strings you have to use equals method

For example:'

"y".equals(check11)
Janar
  • 2,623
  • 1
  • 22
  • 32
  • 3
    Obvious duplicates don't need answers, they need close votes. I don't think you have the rep to close vote yet, but you can identify a duplicate that someone else can use to close the question. (In this case, it's the canonical http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – T.J. Crowder Nov 20 '16 at 10:14
  • That is not the question. The problem is my code. The last while statement will not activate. I'm asking if you know what the problem is. – Gerald Loo Nov 20 '16 at 10:16