Mehreen Malik
Mehreen Malik

Reputation: 23

How to add more to the contents of a String array in java

I'm trying to find a way to add more to an already filled array, the user of the program must select one of the array for example seat[0][1] and then add their name should be added to next to the seat they've chosen. Is there any way of doing this or is there a way of changing the contents of the part they've chosen to their name? I'm using a 2D String array.Here's the code I've written so far if you could please offer any advice I'd be grateful thanks.

{String [][] seat = new String[2][6];

seat[0][0] = "A.1"; 
seat[0][1] = "B.1";
seat[0][2] = "C.1";
seat[0][3] = "D.1";
seat[0][4] = "E.1";
seat[0][5] = "F.1";
seat[1][0] = "A.2";
seat[1][1] = "B.2";
seat[1][2] = "C.2";
seat[1][3] = "D.2";
seat[1][4] = "E.2";
seat[1][5] = "F.2";

//Print out array here using for-loops

System.out.println("Please choose your seat: ");
chosenseat=Keyboard.readString();

System.out.println("Please enter the name for the booking: ");
name=Keyboard.readString();}

Upvotes: 0

Views: 107

Answers (3)

Mehreen Malik
Mehreen Malik

Reputation: 23

Here's the answer I ended up using, The assignment said " The plane will have 30 rows and 6 columns of seats. The rows are indexed by a number 1-30 and the columns are indexed with a letter A-F. Internally your program should represent the seats as a 2D array. When a seat is booked your program should store a name associated with that seat.You can implement this as having a single 2D array of strings for both seat occupancy and name." This answer replaces the A.1 for example with a name.

String chosenseat, name;


System.out.println("The seat map: ");
        String [][] seat = new String[2][3];

        seat[0][0] = "A.1";         
        seat[0][1] = "B.1";
        seat[0][2] = "C.1";

        seat[1][0] = "A.2";
        seat[1][1] = "B.2";
        seat[1][2] = "C.2";

        for(int i=0;i<seat.length;i++)//loop through the rows
            {
                for(int j=0;j<seat[0].length;j++)//loop through the columns
                {
                    System.out.print(seat[i][j]+" ");
                }
                System.out.println();
            }
        String stringToSearch;

        System.out.println("What seat would you like:");
        stringToSearch=Keyboard.readString();

        System.out.println("Please enter your name: ");
        name=Keyboard.readString();

        for (int i = 0; i <seat.length; i++)
        {
            for (int j = 0; j < seat[0].length; j++)
            {
                if (seat[i][j].equals(stringToSearch))
                System.out.print(i);

            }
        }
        for (int i = 0; i <seat.length; i++)
        {
            for (int j = 0; j < seat[0].length; j++)
            {
               if (seat[i][j].equals(stringToSearch))
                    System.out.println("."+j);
            }
        }
        for(int i=0;i<seat.length;i++)//loop through the rows
        {
            for(int j=0;j<seat[0].length;j++)//loop through the columns
            {
                if (seat[i][j].equals(stringToSearch))
                seat[i][j]=name;
            }
        }

        for(int i=0;i<seat.length;i++)//loop through the rows
            {
                for(int j=0;j<seat[0].length;j++)//loop through the columns
                {
                    System.out.print(seat[i][j]+" ");
                }
                System.out.println();
            }
        }
    }

Upvotes: 0

Alexis C.
Alexis C.

Reputation: 93842

the user of the program must select one of the array for example seat[0][1] and then add their name should be added to next to the seat they've chosen

I would create a class to handle that:

class Seat {
    private String name;
    private String bookingName;

    public Seat(String name, String bookingName){
        this.name = name;
        this.bookingName = bookingName;
    }

    public Seat(String name){
        this(name, "unknown");
    }

    /*other stuff (getters, setters, ...)*/
}

Then change your 2D array of Strings to a 2D array of Seats

Seat [][] seats = new Seat[2][6];

seats[0][0] = new Seat("A.1"); 
seats[0][1] = new Seat("B.1");
seats[0][2] = new Seat("C.1");
.....

Finally you will need to loop through the elements of the array. Then for each seat, check if the name of it exists and that the bookingName is not "unknown". Then set the booking name for this seat.

Upvotes: 1

Christian Tapia
Christian Tapia

Reputation: 34146

Arrays are fixed-size so, you should use a Collection like ArrayList:

List<List<String>> arr = new ArrayList<>();

to add a "row":

arr.add(new ArrayList<>());

to add an element to a row:

arr.get(0).add("..."); // add an element to the first row
arr.add(new ArrayList<>()); // add another row
arr.get(1).add("..."); // add an element to the second row

and to get an element:

// ...
arr.get(1).get(0); // get first element of the second row

Note:

  • Remember that indices in most programming languages starts with 0. So to access to the first element you will have to use the index 0.

Upvotes: 2

Related Questions