0

I've got a simple question that I've got to complete for tomorrow. The code compiles and runs but then I get an Exception Error. Sorta Kinda new to java and I have a feeling this is related to multidimensional array.

package question12;

import java.security.SecureRandom;
import java.util.Scanner;



public class Question12 
{

Scanner sc = new Scanner(System.in);

String First_Name, Last_Name, ID, Seat_No;
char seatCh;
String[][] passengerList = new String[1][4];

void setFname()
{
    System.out.printf("Passenger First Name: ");
    First_Name = sc.nextLine();
}

void setLname()
{
    System.out.printf("Passenger Last Name:  ");
    Last_Name = sc.nextLine();
}

void setID()
{
    System.out.printf("Passenger ID:         ");
    ID = sc.nextLine();
}

void setSeatNumber ()
{
    String ticket, StringRandNum;
    String alfabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int seatCharNum, seatIntNum;

    SecureRandom randomN = new SecureRandom();

    seatIntNum = randomN.nextInt(120);
    seatCharNum = randomN.nextInt(7);

    seatCh = alfabet.charAt(seatCharNum);

    Seat_No = "" + seatIntNum;


}

void generateTicket()
{

    for (int i = 0; i < 39; i++)
    {
        if (i == 19)
        {
            System.out.printf("Air KZN");
        } else
            System.out.print('*');
    }

    System.out.println("");

    System.out.printf("*You are seated in seat number: %s%s%n", seatCh, Seat_No);
    System.out.printf("*Passenger name: %s\n", First_Name);
    System.out.printf("*Passenger surname: %s%n", Last_Name);
    System.out.printf("*Passenger ID: %s%n", ID);

    for (int i = 0; i < 39; i++)
    {
        if (i == 19)
        {
            System.out.printf("Air KZN");
        }

        System.out.print('*');
    } 

    System.out.println("");

}

void storeValues()
{
    passengerList[0][0] = First_Name;
    passengerList[0][1] = Last_Name;
    passengerList[0][2] = ID;
    passengerList[0][3] = Seat_No;
}

void printPassengerList()
{
    System.out.printf("Passenger Name   Passenger Surname   Passenger ID    Seat Number\n");

    for (int a = 0; a < 40; a++)
        System.out.print('-');

    for (int c = 0; c < passengerList.length; c++)
        for (int d = 0; d < passengerList[d].length; d++)
        System.out.print(passengerList[d][c]);
}

public static void main(String[] args) 
{

    Question12 q12 = new Question12();

    q12.setFname();
    q12.setLname();
    q12.setID();
    q12.setSeatNumber();
    q12.generateTicket();
    q12.storeValues();
    q12.printPassengerList();

    }

}
Yeezus
  • 397
  • 1
  • 3
  • 14
  • `for(int d = 0; d < passengerList[d].length; d++)` looks fishy. At what line is your error, and what line of code does that correspond to in your post here? – RaminS Apr 21 '16 at 19:31
  • What line is the error on? – NRitH Apr 21 '16 at 19:31
  • 1
    `passengerList[d].length` shouldn't this be `passengerList[c].length` ? Also `System.out.print(passengerList[d][c]);` seems backwards. – Clark Kent Apr 21 '16 at 19:32
  • [This](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) should be the starting point before starting the question in SO. – randominstanceOfLivingThing Apr 21 '16 at 19:32
  • Your `passengerList` only ever contains a single element--an array of 4 strings. – NRitH Apr 21 '16 at 19:33
  • `ArrayIndexOutOfBoundsException` will be thrown only when you try to access index that beyond the valid range of an array. For example if you have an array with length of `n`, so the valid index range will start from `0` and end at `n-1`. Any access that is greater than `n-1` or smaller than `0` will throw this exception. You need to consider carefully about this valid range of your array, especially when you have multi-dimensions – Rugal Apr 21 '16 at 19:38
  • @Saviour Self . Thumbs UP!! – Yeezus Apr 21 '16 at 19:43

1 Answers1

1

You have:

for (int c = 0; c < passengerList.length; c++)
    for (int d = 0; d < passengerList[d].length; d++)
        System.out.print(passengerList[d][c]);

This will give you an error because of two reasons:

  • Your inner loop should have passengerList[c].length instead of passengerList[d].length.

  • Your printout should be passengerList[c][d] instead of passengerList[d][c}.

Change it to this:

for (int c = 0; c < passengerList.length; c++)
    for (int d = 0; d < passengerList[c].length; d++)
        System.out.print(passengerList[c][d]);
RaminS
  • 2,208
  • 4
  • 22
  • 30