1

I have been given the passenger list with passengers names, which represents the passengers currently waiting in the waiting room (which is an array).

Currently, I am struggling with some parts of step 1. The pseudocode is as follows:

  1. Read in the passengers.dat file into the waitingRoom array.

Here is my code:

    public class TrainStation {
    int WAITING_ROOM_CAPACITY = 30;

       static Passenger[] waitingRoom = new Passenger[WAITING_ROOM_CAPACITY]; 

       static PassengerQueue queue = new PassengerQueue();

       public static void main(String[] args) throws IOException {
       //1. 
            try {
            File pointFile = new File ("passenger.dat");
            Scanner pointReader = new Scanner (pointFile);
            String firstName, surname;
            int i = 0;
            while (pointReader.hasNext()) {
                firstName = pointReader.next();
                surname = pointReader.next();
                Passenger Object = new Passenger(firstName, surname);
                waitingRoom[i] = Object;
                i++;
            }   
                for (int fori=0; fori<WAITING_ROOM_CAPACITY; fori++){
                System.out.println(waitingRoom[fori]); }
            pointReader.close();

          } catch (IOException e) {
            System.out.println("Sorry, file not found");  
          }

My WAITING_ROOM_CAPACITY can only hold 30 passengers I created two arrays. One array is for the waitingRoom and the other one for the trainQueue. I tried to read the file using the Scanner and following the pseudocode by converting the Passenger class into an object, however, I keep getting "example.Passenger@6bc7c054" rather than reading the actual names of the passengers from the file.

enter image description here

In addition to this, I have two classes:

package trainstation;


public class Passenger {
    private String firstName; 
    private String surname;
    private int secondsInQueue;

    Passenger(String firstName, String surname) {
        this.firstName = firstName;
        this.surname = surname; 
        secondsInQueue = 0;
    }

This is my Passenger Class, where I allow within my add method to add a passenger with their names and surnames.

Could someone please help as I am really struggling.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Please check out the [ask] for more on how to improve this and your future questions – Hovercraft Full Of Eels Mar 29 '20 at 18:04
  • 3
    Try breaking down each of those steps into separate methods. (This is a basic technique sometimes called "divide and conquer".) Then figure out what is not working with each step and ask a question about just that step. – markspace Mar 29 '20 at 18:07
  • Regarding, "I tried to read the (passenger.dat) file using the Scanner and following the pseudocode by converting the Passenger class into an object, however, I keep getting some errors." -- ***what*** errors? The error messages are of extreme importance. Again, please read the [ask] as it will tell you this. – Hovercraft Full Of Eels Mar 29 '20 at 18:18
  • 1
    This question is a duplicate of [How do I print my Java object without getting “SomeType@2f92e0f4”?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Hovercraft Full Of Eels Mar 29 '20 at 19:08
  • Does this answer your question? [How do I print my Java object without getting "SomeType@2f92e0f4"?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – rene Mar 29 '20 at 20:04

1 Answers1

0

First of all, you can add Errors, Logs and anything your console outputs as well as text as your code snippets.

If an error does not clearly state, info, warning, error or exception, it is neither of those.

What I see by your picture is just a String representation of your Passenger entry in your array.

I try to clarify:

Passenger is an object. By System.out.println(passenger); you just tell your compiler that you want to print that passenger, but your compiler cant say what you want to print exactly, so it tells you what it is. A Passenger created by example with an hexnumber for identification. It doesn't know you want to print that String which is only a PART of that object, even if its propably 99% of that object.

Ok, now how to fix it, quite easily, in your Passenger class just add:

public String toString() { 
   return this.firstName + "," + this.surname;
}  

Because that System.out.println() will just use the toString() method to print.

Jan Uhlig
  • 82
  • 5
  • 1
    This question is a duplicate of [How do I print my Java object without getting “SomeType@2f92e0f4”?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Hovercraft Full Of Eels Mar 29 '20 at 19:09
  • 2
    Better to vote as a duplicate than to answer it. To wit -- please compare your answer to the ones in the duplicate and tell which ones are more helpful to future visitors with a similar problem? – Hovercraft Full Of Eels Mar 29 '20 at 19:09
  • @HovercraftFullOfEels how does one _vote as a duplicate_ ? (Please pardon my ignorance.) – Abra Mar 29 '20 at 19:17