Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Reading from a text file into an ArrayList

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Reading from a text file into an ArrayList

    For the following problem, I have created the passenger class and have created the ArrayList in the Train Class. I do not know how to read the passengers from a text file into the ArrayList. And I also do not understand how to do the other methods. Help would be greatly appreciated.

    Train Class
    import java.util.ArrayList;
    public class Train
    {
        private ArrayList<Passenger> train;
     
        public Train()
        {
            train = new ArrayList<Passenger>();
        }
    }

    Passenger Class
    public class Passenger
    {
        private int serviceClass;
        private String name;
     
        public Passenger(int sC, String n)
        {
            setServiceClass(sC);
            setName(n);
        }
     
        public void setServiceClass(int sC)
        {
            if(sC == 1 || sC == 2)
                serviceClass = sC;
            else
                System.out.println("Error");
        }
     
        public void setName(String n)
        {
            name = n;
        }
     
        public String getName()
        {
            return name;
        }
     
        public int getServiceClass()
        {
            return serviceClass;
        }
    }

    Design and code a program including the following classes, as well as a client class to test all the methods coded:

    A Passenger class, encapsulating a passenger. A passenger has two attributes: a name, and a class of service, which will be 1 or 2.

    A Train class, encapsulating a train of passengers. A train of passengers has one attribute: a list of passengers, which must be represented with an ArrayList. Your constructor will build the list of passengers by reading date from a file called passengers.txt (that you would create). You can assume that passengers.txt has the following format:
    <name1> <class1>
    <name2> <class2>
    ….

    For instance, the file could contain:

    James 1
    Ben 2
    Suri 1
    Sarah 1
    Jane 2
    …..
    You should include the following methods in your Train class:
    A method returning the percentage of passengers traveling in the first class
    A method taking two parameters representing the price of traveling in first and second class and returning the total revenue for the train.
    A method checking if a certain person is on the train; if he/she is, the method returns true; otherwise, it returns false.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Reading from a text file into an ArrayList

    Quote Originally Posted by Spanky_10 View Post
    I do not know how to read the passengers from a text file into the ArrayList.
    Read the file line by line and add the passengers to the list as you go.

    Quote Originally Posted by Spanky_10 View Post
    And I also do not understand how to do the other methods.
    A method returning the percentage of passengers traveling in the first class
    A method taking two parameters representing the price of traveling in first and second class and returning the total revenue for the train.
    A method checking if a certain person is on the train; if he/she is, the method returns true; otherwise, it returns false.
    The first two require collecting the required values and doing some math. Figure out how to solve these on paper before you write the code.
    For the last one you will need to decide how to check the list to see if a specific name is on the list.

Similar Threads

  1. Not Reading From Text File
    By JavaLaxer15 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2012, 11:16 AM
  2. Reading a text file into an arraylist of fueldispensers.
    By vbhatti in forum Object Oriented Programming
    Replies: 3
    Last Post: November 20th, 2011, 01:17 PM
  3. reading string in from text file
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 3rd, 2010, 05:31 PM
  4. Reading a lines from text file
    By kiran in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 26th, 2010, 10:54 PM
  5. [SOLVED] Reading from a text file and storing in arrayList
    By nynamyna in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 09:55 PM