Hello everyone. I'm currently having a problem with choosing one area in an array. For what I'm working on I'm trying to pick one seat in an array consisting of 10 seats. The first 5 seats are firstclass and the last 5 seats are economy. For example if the person picks firstclass they should be issued one seat out of the five but my program keeps selecting 5 seats. Here is an example of what I'm doing.

import java.util.Scanner;

public class Flight
{

    public static void main(String[] args)
    {

      Scanner input = new Scanner(System.in);
      int sectionnumber;
      String selectionletter;
      int seats[] = new int[9];


      System.out.println ("*************** Airline Reservation System ****************");



      System.out.println ("Please type 1 for First Class or 2 for Economy");
      sectionnumber = input.nextInt();

		// Lets user select first class or economy
      if (sectionnumber == 1)
      {
        for (int firstclass = 1; firstclass < 6; firstclass++)
        {

			System.out.println("Your seat number is: " + firstclass);
		}
      }
      if (sectionnumber ==2)
      {
		for (int economy = 6; economy < 11; economy++)
		{
			System.out.println("Your seat number is: " + economy);
		}
      }

Recommended Answers

All 5 Replies

hi,
i think you should construct your program differently , the reason behind is the for loops. When the user enters 1, for the first class , it will go to

for (int firstclass = 1; firstclass < 6; firstclass++)        { 			System.out.println("Your seat number is: " + firstclass);		}

Where you are issuing all the five tickets associated to first class.

How can I go about doing so. Arrays are my weakest areas. I have the Java How to Program Seventh addition book which goes into details about arrays but doesn't have an example where it actually asks the user to input information and then access the array to match it up. All it has are examples of data already created to put into an array. Thanks in advance.

It can be done without using arrays but if you have to use it then , do you have to actually store the name of the person in an array element.
For example
Adam = seat[0];
like this ?
Anyway i have done a code without array check it , if it might give some help

import java.util.*;

class Flight {

   Scanner in = new Scanner(System.in);
   
   void issue ()
   {
   	 int sectionnumber;
   	 int count=1;
   	 int count1=1;
   	 System.out.print("\n**********AirLine Program***********\n");
   	 for(int i = 0 ;i<=9;i++)
   	 {
   	  System.out.print("\nPlease type 1 for First Class and 2 for Second class");
   	 sectionnumber = in.nextInt();
   	 if (sectionnumber==1)
   	 {  
   	 	if(count >=6)
   	 	{
   	    	System.out.print("\nNo more seats available in first class");		
   	 	    
   	 	}
   	 	else
   	 	{
   	 	
   	 	System.out.print("\nYour seat number is:"+count);
   	 	count++;
   	 	}
   	 }
   	 
   	 else if (sectionnumber ==2)
   	 {
   	 		if(count1 >=6)
   	 	{
   	    	System.out.print("\nNo more seats available in Second class");		
   	
   	 	}
   	 	else
   	 	{
   	 	
   	 	System.out.print("\nYour seat number is:"+count1);
   	 	count1++;
   	 	}
   	 }
   	 
   	 }
   }
}
commented: helpful +7

Thanks alot. It helped. The part where I need a boolean array is to show a seating chart on the plane and have it show which seats are available. I think I'll be able to take it from here. Thanks again for the help, I really appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.