943,948 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 667
  • Java RSS
Jul 15th, 2008
0

Array Selections

Expand Post »
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.

java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
NycNessyness is offline Offline
58 posts
since Aug 2007
Jul 15th, 2008
0

Re: Array Selections

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

Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 16
Solved Threads: 18
Junior Poster
Software guy is offline Offline
151 posts
since May 2008
Jul 15th, 2008
0

Re: Array Selections

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
NycNessyness is offline Offline
58 posts
since Aug 2007
Jul 15th, 2008
1

Re: Array Selections

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
Java Syntax (Toggle Plain Text)
  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
Reputation Points: 16
Solved Threads: 18
Junior Poster
Software guy is offline Offline
151 posts
since May 2008
Jul 15th, 2008
0

Re: Array Selections

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
NycNessyness is offline Offline
58 posts
since Aug 2007
Jul 15th, 2008
0

Re: Array Selections

Thats cool :)
Reputation Points: 16
Solved Threads: 18
Junior Poster
Software guy is offline Offline
151 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Conference Centre
Next Thread in Java Forum Timeline: Need help in stroke and shape





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC