0

I am trying to create a program that generates plane tickets and after that it asks the user if they want to see the passenger list.

What I am struggling with is that after every user that has input their details, they must get added to the passenger list. So if User1 enters his details and must get asked if they want to see the passenger list (so far there will be only one record under the passenger list), then wen User2 enters his details it must ask the user if they want to see the passenger list and the User2 details must get added to the passenger list (so we will have 2 records under the passenger list) and so on..

Please assist with that.

public class Question12 {

    public static void main(String[] args) {
               double random = Math.random()* 69 + 1;
               String q;
        Scanner in = new Scanner(System.in);
        String[][] PlaneTicket = new String[3][3];

       for (int row = 0; row < 67; row++) {


           System.out.print("Enter name:");        
           PlaneTicket[row][0] = in.next();

           System.out.print("Enter surname: ");
           PlaneTicket[row][1] = in.next();


           System.out.print("Enter a ID: ");
           PlaneTicket[row][2] = in.next();

           System.out.println("************Airport**********");
           System.out.println("Seat number: " +  Math.round(random));
           System.out.println("Name: " + PlaneTicket[row][0]);
           System.out.println("Surname: " + PlaneTicket[row][1]);
           System.out.println("ID: " + PlaneTicket[row][2]);
           System.out.println("*****************************");

           System.out.println("Do you want to view the passanger list: " + "(Yes/No)");
           q = in.next();

          if ("Yes".equals(q))  {
           System.out.println("Name     Surname     ID     Seat Number");
           System.out.println("----------------------------------------");
           System.out.println(PlaneTicket[row][0]+"    "+PlaneTicket[row][1]+"      "+PlaneTicket[row][2]+ "    "+ Math.round(random));   
6
  • Your code is cut off at the bottom... Commented Mar 23, 2016 at 22:28
  • 1
    here your case you can store only first 3-passanger info then after you will get ArrayIndexOutOfBoundException. Commented Mar 23, 2016 at 22:31
  • Sorry, that is all the code i have. It is just missing a bracket. Commented Mar 23, 2016 at 22:32
  • It misses at least 3 brackets!? if, for, and main-method. Commented Mar 23, 2016 at 22:33
  • @Lyubo On a side note: use shall for fundamental requirements instead of must and should for nice-to-be-there requirements. I edited your question. Commented Mar 23, 2016 at 22:33

3 Answers 3

1

If you just want to print all the entered values so far, you can do:

System.out.println("Name     Surname     ID     Seat Number");
System.out.println("----------------------------------------");

for (int k = 0; k <= row; k++) {   
    System.out.println(PlaneTicket[k][0] + "    " + PlaneTicket[k][1] + "      " + PlaneTicket[k][2] + "    " + Math.round(random));
}

Does that answer your question?

Some remarks:

Here is your main method with some fixes, as mentioned please do some redesign (see my remarks):

public static void main(String[] args) {
    final int maxPassengers = 67;

    double random = Math.random() * 69 + 1;
    String q;
    Scanner in = new Scanner(System.in);
    String[][] PlaneTicket = new String[maxPassengers][4];

    for (int row = 0; row < maxPassengers; row++) {
        System.out.print("Enter name:");
        PlaneTicket[row][0] = in.next();

        System.out.print("Enter surname: ");
        PlaneTicket[row][1] = in.next();

        System.out.print("Enter a ID: ");
        PlaneTicket[row][2] = in.next();

        // you should keep the SeatNumber
        PlaneTicket[row][3] = String.valueOf(Math.round(random));

        System.out.println("************Airport**********");
        System.out.println("Seat number: " + PlaneTicket[row][3]);
        System.out.println("Name: " + PlaneTicket[row][0]);
        System.out.println("Surname: " + PlaneTicket[row][1]);
        System.out.println("ID: " + PlaneTicket[row][2]);
        System.out.println("*****************************");

        System.out.println("Do you want to view the passanger list: " + "(Yes/No)");
        q = in.next();

        if ("Yes".equalsIgnoreCase(q) || "y".equalsIgnoreCase(q)) {
            System.out.println("Name     Surname     ID     Seat Number");
            System.out.println("----------------------------------------");

            for (int k = 0; k <= row; k++) {
                System.out.println(PlaneTicket[k][0] + "    " + PlaneTicket[k][1] + "      " + PlaneTicket[k][2] + "    " + PlaneTicket[row][3]);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. I just tested it and it works perfectly.
I did it, but since you added the remark about the array size, I revoked it. +1
0

You initialize a two dimensional array PlaneTicket = new String[3][3] and the you run it up to 66.. that clearly gives an IndexOutOfBounds Exception. Best way, avoid magic numbers and put them in a variable such as row or column and then iterate up to it.

Otherwise it looks OK.

Second, be careful with the terms list and array. Do you mean Passengerlist or just an array?

It seems you have it at the bottom, but you miss some brackets. After fixing the indexing, it should work.

Comments

0

You need to create a method displayList(String[][] seats) and call it once each customer types "Yes". This method will have similar loop(s) and display the current list

  for (int row = 0; row < 67; row++) { ....}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.