Hello,

Is it possible for anyone to explain to me what

"Initialize the 3 parallel arrays" means?

Here is the problem.

A file called seats.txt contains a description of an airline seating arrangement and a list of individual seat assignments. An example is shown here:

rows 7
leftSeats 1
rightSeats 2
1-B-L.Bleriot 1-C-A.Earhart
2-B-W.Post 2-C-H.Hughes
1-A-C.Lindbergh
2-A-H.Quimby
6-C-G.Curtiss
4-C-K.Tank 4-B-B.Hoover

This example indicates a plane has 7 rows. In a given row, there is 1 seat on the left and 2 seats on the right separated by an aisle. Write a Java program that reads in this file and produces a visual representation of the seat assignments, with a passenger's name and his/her seat assignment in each seat slot. The above example would produce the following output:

A B C
1 [ C.Lindbergh 1A] [ L.Bleriot 1B] [ A.Earhart 1C]
2 [ H.Quimby 2A] [ W.Post 2B] [ H.Hughes 2C]
3 [ ] [ ] [ ]
4 [ ] [ B.Hoover 4B] [ K.Tank 4C]
5 [ ] [ ] [ ]
6 [ ] [ ] [ G.Curtiss 6C]
7 [ ] [ ] [ ]

Notice the space representing the aisle between seats A and seats B and C.

To keep things manageable, the airplane will always have 3 seats, either 1 on the left and two on the right, or 2 on the left and 1 on the right. Hints:

Use the "split()" method in the Java String class to split the assignment string into a row, seat, and passenger name.

Use the System.out.format() method to produce fixed width "cells" for each seat. For example, the

out.format("[%15s]", passenger.toString());

produces space for 15 characters in a passenger's name.

You can use either a 2-dimensional array or three parallel arrays to make the task easier.

Here is a main class to start with.

Here is a Passenger.java class which can be used.

HERE IS THE CODE for the Main Class

package csc212project03b;
/**
 * File: Main.java
*/

import static java.lang.System.out;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

public class Main
{

    public static void main(String[] args) throws IOException
    {
        Scanner reader;

        out.println("Welcome to Plane Talk ...");
        out.println("\ta program to display a passenger seating arrangement. ");

        reader = new Scanner(new FileInputStream("seats.txt"));

        // Read number of rows
        // Read number of seats













        // Initialize the 3 parallel arrays (or 1 2-D array)





        while ( reader.hasNext() ) {
            // read and split data






            // once you have the data, uncomment next line
            // Passenger pas = new Passenger(name, row, seat);

            // Place in appropriate list






        }

        // print output header



        // loop for i going from 1 to number of rows

            // print the three lists, position i




        // end of for loop

   }
}

OTHER CLASS

package csc212project03b;
/**
 * File: Passenger.java
*/

public class Passenger
{
    private String name;
    private int row;
    private char seat;

    public Passenger(String name, int row, char seat)
    {
        this.name = name;
        this.row = row;
        this.seat = seat;
    }

    public String toString()
    {
        return name + " " + row + seat;
    }
}

I just gave you the whole assignment that way if it was needed to explain to me how I would initialize the 3 parallel array. Just looking for help or a hint. I don't want the whole answer as I am supposed to learn from this, but I have no idea how to initialize the 3 parallel arrays. Thanks.

Recommended Answers

All 12 Replies

Since you either should use a 2D array or 3 "parallel" arrays that must mean that you use 3 1D arrays to store each column of seats.

One array would then contain [ [C.Lindbergh 1A], [H.Quimby 2A], ..., [] ] etc. I would go with a 2D array since that would be more general for amount of rows and columns.

Since you either should use a 2D array or 3 "parallel" arrays that must mean that you use 3 1D arrays to store each column of seats.

One array would then contain [ [C.Lindbergh 1A], [H.Quimby 2A], ..., [] ] etc. I would go with a 2D array since that would be more general for amount of rows and columns.

So do I use a for loop to fill the array? ex

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

Yes, you would initialize based on the number of rows.

I'm so confused I am honestly not sure how I would start this. I assume when it says read it it means to type. String row = reader.next(); is that correct. I did the for loop and it says I need to make an array. hmm could someone get me started here? please and thanks.

I'm so confused I am honestly not sure how I would start this. I assume when it says read it it means to type. String row = reader.next(); is that correct. I did the for loop and it says I need to make an array. hmm could someone get me started here? please and thanks.

This is what I have thus far and I'm not sure if I'm even going in the right direction. I can't stare at it any longer I need to walk away and come back.

public static void main(String[] args) throws IOException
    {
        Scanner reader;

        out.println("Welcome to Plane Talk ...");
        out.println("\ta program to display a passenger seating arrangement. ");

        reader = new Scanner(new FileInputStream("seats.txt"));
private static String[] row = null; 

        String row = reader.next();
        String seat = reader.next();
        reader.next(); 
        // Read number of rows
        // Read number of seatz


row = new String[]; 









        // Initialize the 3 parallel arrays (or 1 2-D array)
        for (int i = 0; i < row.length(); i++) {
            row[i] = row;  
        }

I guess you should start from how to read in data from a text file. You could look at this example. Then what you need to do is to parse the data (all lines after rightSeat line) in those 3 parallel arrays as stated by Ezzaral. While reading the data from file, you should simultaneously fill in your arrays. Once you are done reading, you can now display the result using row number.

By the way, to convert an integer string (when you read in from text file), you need to use Integer class with parseInt() method ( i.e. Integer.parseInt(integerString); ). The method will return an integer.

I have to read in with a scanner and that example uses a buffer. We have not learned about them. How would I do it as a scanner? Do I read them in as Strings then parse int.

I really just need to figure out how to start this. It keeps saying I have no array. I am not sure what I need to do.

public static void main(String[] args) throws IOException
    {
        Scanner reader;

        out.println("Welcome to Plane Talk ...");
        out.println("\ta program to display a passenger seating arrangement. ");

        reader = new Scanner(new FileInputStream("seats.txt"));

        
        
        String row = reader.next();
        String seat = reader.next();
int iRow = Integer.parseInt(row);
int iSeat = Integer.parseInt(seat); 

for(int i = 0; i < row.length(); i ++) {
    iRow[] =
}
        // Read number of rows
        // Read number of seat

Am I going in the right direction to read these in and parse the ints?

I think I have been missing the while

public static void main(String[] args) throws IOException
    {
        Scanner reader;

        out.println("Welcome to Plane Talk ...");
        out.println("\ta program to display a passenger seating arrangement. ");

        reader = new Scanner(new FileInputStream("seats.txt"));

        
 while (reader.hasNext()) {
            String[] row;
            String[] seat;
        // Read number of rows
        // Read number of seat

if I keep going in this direction will I be going the right way?

I'm so confused. Wish I knew more about arrays.

Yes that it what you want to do. Loop through the scanner and place the seating information into the correct places in the arrays.

I think it will be easier if you construct a 2D array and only store the name of the passenger in each index. If the plane has 3 seats on a row and 7 rows you can create the following array.

String[][] passengers = new String[7][3]; //[rows][leftSeats+rightSeats]

Then passenger C.Lindbergh 1A would be placed in passengers[0][0] (row 0, seat 0), passenger L.Bleriot 1B in passengers[0][1] [row 0, seat 1] and so on. The seat and row name can be derived from the indexes in the array. passengers[0][X] is row 1. passengers[X][1] is row X seat B. You can of course also store the seat information along with the passengers.

All you need to to then is to go trough the file. parse out each passenger and the seat, and place them in the correct place in the 2D-array.

pseudocode:

construct 2D-array passengers[rows][seatsPerRow]

for each passenger p do  //your while loop

      String[] passengerInfo = split p with "-" delimiter.

      row = convert passengerInfo[0] to integer - 1. (-1 since arrays indexes start from 0. Lookup       Integer.parseInt() in java api).

      seat = passengerInfo[1]

      name = passengerInfo[2]

      seatNumber = convert seat string to number // if (seat is "B") seatNumber = 1

      passenger[row][seatNumber] = name.  // place the passengers name in the correct place.

When you have done this you can just loop through the array to print the passengers. You will a loop in a loop.

for ( int row = 0; i < passengers.length; row++ ) {
     for( int seat = 0; seat < passengers[row].length; seat++ ) {
          //print passenger[row][seat] + seat info
     }
     //newline
}

This should give you almost everything you need to do.

Refer to this page for Scanner api. you may want to use nextLine() to obtain each line. I may be wrong because I don't use Scanner to read file. One note that I am not sure if a file that ends without a new empty line at the end would be produced by nextLine(). Also, I am not sure how hasNext() would deal with the case either...

But you are going to the right direction about reading in; however, remember that using nextLine() would give you a whole line string back. Not sure about hasNext(). So if each time of the loop is a line string, you must have a line count in there in order to keep track of where which line you are reading at the moment. Also, your row & seat variable should be outside of the loop because their values will be changed after the loop finishes. If you declare it inside the loop, it will become local to the loop and will not be usable after the loop finish.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.