Reading a text file into a one dimensional array

kkkkkk :

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.

Jan Uhlig :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=370563&siteId=1