Array Selections

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2007
Posts: 58
Reputation: NycNessyness is an unknown quantity at this point 
Solved Threads: 0
NycNessyness NycNessyness is offline Offline
Junior Poster in Training

Array Selections

 
0
  #1
Jul 15th, 2008
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.

  1. import java.util.Scanner;
  2.  
  3. public class Flight
  4. {
  5.  
  6. public static void main(String[] args)
  7. {
  8.  
  9. Scanner input = new Scanner(System.in);
  10. int sectionnumber;
  11. String selectionletter;
  12. int seats[] = new int[9];
  13.  
  14.  
  15. System.out.println ("*************** Airline Reservation System ****************");
  16.  
  17.  
  18.  
  19. System.out.println ("Please type 1 for First Class or 2 for Economy");
  20. sectionnumber = input.nextInt();
  21.  
  22. // Lets user select first class or economy
  23. if (sectionnumber == 1)
  24. {
  25. for (int firstclass = 1; firstclass < 6; firstclass++)
  26. {
  27.  
  28. System.out.println("Your seat number is: " + firstclass);
  29. }
  30. }
  31. if (sectionnumber ==2)
  32. {
  33. for (int economy = 6; economy < 11; economy++)
  34. {
  35. System.out.println("Your seat number is: " + economy);
  36. }
  37. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 38
Reputation: Software guy is an unknown quantity at this point 
Solved Threads: 2
Software guy Software guy is offline Offline
Light Poster

Re: Array Selections

 
0
  #2
Jul 15th, 2008
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

  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 58
Reputation: NycNessyness is an unknown quantity at this point 
Solved Threads: 0
NycNessyness NycNessyness is offline Offline
Junior Poster in Training

Re: Array Selections

 
0
  #3
Jul 15th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 38
Reputation: Software guy is an unknown quantity at this point 
Solved Threads: 2
Software guy Software guy is offline Offline
Light Poster

Re: Array Selections

 
1
  #4
Jul 15th, 2008
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
  1. import java.util.*;
  2.  
  3. class Flight {
  4.  
  5. Scanner in = new Scanner(System.in);
  6.  
  7. void issue ()
  8. {
  9. int sectionnumber;
  10. int count=1;
  11. int count1=1;
  12. System.out.print("\n**********AirLine Program***********\n");
  13. for(int i = 0 ;i<=9;i++)
  14. {
  15. System.out.print("\nPlease type 1 for First Class and 2 for Second class");
  16. sectionnumber = in.nextInt();
  17. if (sectionnumber==1)
  18. {
  19. if(count >=6)
  20. {
  21. System.out.print("\nNo more seats available in first class");
  22.  
  23. }
  24. else
  25. {
  26.  
  27. System.out.print("\nYour seat number is:"+count);
  28. count++;
  29. }
  30. }
  31.  
  32. else if (sectionnumber ==2)
  33. {
  34. if(count1 >=6)
  35. {
  36. System.out.print("\nNo more seats available in Second class");
  37.  
  38. }
  39. else
  40. {
  41.  
  42. System.out.print("\nYour seat number is:"+count1);
  43. count1++;
  44. }
  45. }
  46.  
  47. }
  48. }
  49. }
Last edited by Software guy; Jul 15th, 2008 at 5:06 pm. Reason: Some thing different in code
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 58
Reputation: NycNessyness is an unknown quantity at this point 
Solved Threads: 0
NycNessyness NycNessyness is offline Offline
Junior Poster in Training

Re: Array Selections

 
0
  #5
Jul 15th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 38
Reputation: Software guy is an unknown quantity at this point 
Solved Threads: 2
Software guy Software guy is offline Offline
Light Poster

Re: Array Selections

 
0
  #6
Jul 15th, 2008
Thats cool :)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC