#How to assign numbers in a boolean array

51 messages · Page 1 of 1 (latest)

stable merlin
#

basically i have this variable
boolean[] firstClass = new boolean[11];
I need to store the numbers 1,2,3,4,5 in that and in another
boolean[] economy = new boolean[11];
I need to store the numbers 6,7,8,9,10 in that with a for loop

twin vesselBOT
#

This post has been reserved for your question.

Hey @stable merlin! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

stable merlin
#

if my coding prompt helps any better here it is

onyx shell
#

for (int i = 0; i < 5; i++) {
firstClass[i] = true;
economy[i + 5] = true;
}

#

the for loop iterates 5 times, and in each iteration, the value of i increments by 1. The value of i is used to store true in the firstClass array at index i and in the economy array at index i + 5.

stable merlin
#

thanks i will try it

stable merlin
#

how would I print that out

#

the numbers

#

right now i have

#

for(int x=0;x<=4;x++){
System.out.println(firstClass[x]);
}

#

to print it out

#

but it just prints out
false
false
false

idle linden
#

well. didn't you just make them false

stable merlin
#

yes

idle linden
#

god why is it in comic sans

#

first off; you shouldn't have 2 arrays

#

you should just have one, of size 10

stable merlin
#

ok

onyx shell
#

for (int x = 0; x <= 4; x++) {
if (firstClass[x]) {
System.out.println(x + 1);
}
}

#

can u try that

idle linden
#

please don't spoonfeed

onyx shell
#

i didn't

stable merlin
#

its fine

onyx shell
#

not the same code

idle linden
#

you literally did. that's what spoonfeeding is

idle linden
onyx shell
#

then you can help

#

Good evening

idle linden
#

sure

#

thank you

onyx shell
#

not matter

idle linden
stable merlin
#

ok i did that

idle linden
#

do you have your input/output system setup

stable merlin
#

yes

idle linden
#

what do you have so far

#

please don't use screenshots, see #❗︱how-to-get-help #6

stable merlin
#

ok

#
import java.text.*;

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

        String bookAnother="y";
        int seat, choice;
        boolean[] firstClass = new boolean[11];
        boolean[] economy = new boolean[11];
        //int firstClass[]={1,2,3,4,5};
        //int economy[]={6,7,8,9,10};
            
        System.out.println("Welcome to the Airline Reservation System\n");

        do{
            System.out.print("Please enter 1 for First Class or enter 2 for Economy: ");
            seat=sc.nextInt();

            if(seat==1){
                System.out.print("The list of available seats in First Class are:\n");
                for(int x=0;x<=4;x++){
                    //firstClass[x] = false;
                    System.out.println(firstClass[x]);                    
                }
                System.out.print("Choose an available seat: ");
                choice=sc.nextInt();
                if((choice!=1)||(choice!=2)||(choice!=3)||(choice!=4)||(choice!=5)){
                    System.out.println("That seat is not available");
                }
                if((choice==1)||(choice==2)||(choice==3)||(choice==4)||(choice==5)){
                    System.out.println("You chose seat "+choice);
                }
            }

            if(seat==2){
                System.out.print("The list of available seats in Economy are:\n");
                for(int y=0;y<=4;y++){
                    //economy[y]=false;
                    System.out.println(economy[y]);                    
                }
                System.out.print("Choose an available seat: ");
                choice=sc.nextInt();
                if((choice!=6)||(choice!=7)||(choice!=8)||(choice!=9)||(choice!=10)){
                    System.out.println("That seat is not available");
                }
#
                    System.out.println("You chose seat "+choice);
                }
                System.out.print("\nWould you like to buy another seat(y/n)? ");
                bookAnother=sc.next();
                if(bookAnother.equalsIgnoreCase("y")){
                    continue;
                }
                if(bookAnother.equalsIgnoreCase("n")){
                    System.out.print("\nThank you for using the Airline Reservation System.");
                    break;
                }
            }
        }while(bookAnother!="n");
    }
}
/*

 */
idle linden
#

💀

#

those conditions yikes

stable merlin
#

man im just startin out

idle linden
#

yeah ik

onyx shell
#

hello

idle linden
#

oh please don't

onyx shell
#

what should i dont

idle linden
#

the boolean array can be used to check if a certain seat x is reserved by using array[x]