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 4 of 4

Thread: Reading into an ArrayList from a text file

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

    Default Reading into an ArrayList from a text file

    I am trying to take a text file and read it into the ArrayList I created. It should store the passengers name and the passenger's service class.

    The text file has the following format:
    James 1
    Sara 2
    Rita 1
    ....etc

    Here is my 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;
        }
    }

    Here is my Train Class:
    import java.io.*;
    import java.util.Scanner;
    import java.util.ArrayList;
    public class Train
    {
        private ArrayList<Passenger> train;
        private double percent;
        private double revenue;
     
        public Train()
        {
            train = new ArrayList<Passenger>();
            File file = new File("Passengers.txt");
            Scanner input = new Scanner(file);
            String name; 
            int sClass;
     
            while(input.hasNext())
            {
                name = input.next();
                sClass = input.nextInt();
                Passenger pass = new Passenger(sClass, name);
                train.add(pass);
            }
            input.close();
        }
     
        double percent_first_class()
        {
            int count;
            int total;
            int serviceClass = pass.getServiceClass();
            if(serviceClass == 1)
            {
                count++;
                total++;
            }
            else
            {
                total++;
            }
            percent = count/total*100;
     
            return percent;
        }
     
        double train_revenue(double price_fC, double price_sC)
        {
            int nFC;
            int nSC;
            int serviceClass = pass.getServiceClass();
            if(serviceClass == 1)
            {
                nFC++;
            }
            else
            {
                nSC++;
            }
            revenue = (price_fC * nFC) + (price_sC * nSC);
            return revenue;
        }
     
        boolean pass_check()
        {
            ArrayList<Passenger> searchforpass = new ArrayList<Passenger>();
            for(Passenger currentPassenger : train)
            {
                if((currentPass.getName()).indexOf(searchString) != -1)
                    searchforpass.add(currentPass);
            }
            searchforpass.trimToSize();
            return true;
        }
    }

    I believe I am inputting the passengers into the ArrayList correctly (but not 100% sure). Then, in my percent_first_class method, I get an error when I try to compile saying that in my line:
    int serviceClass = pass.getServiceClass();
    It cannot find the variable pass

    The train_revenue method will return the total revenue of the train from given inputs in the parameters. I believe that is correct but once again it cannot find the variable pass.

    The pass_check method will allow the user to search for a passenger and will return true if the name is found, and false otherwise. I also believe that is correct.

    If you find mistakes, it would be greatly appreciated if you showed me how to fix them. I am very new to Java yet and am still learning. Thank you to all who help with this.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reading into an ArrayList from a text file

    cannot find the variable pass
    Is the variable: pass defined in the same scope (within the same enclosing {}s) as where it is being used?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Reading into an ArrayList from a text file

    Pass is what I believe is being added to the ArrayList, which is defined in the default constructor. So it is not in the same scope but I do not know how to bring them out of that ArrayList to read the contents and perform the methods.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reading into an ArrayList from a text file

    how to bring them out of that ArrayList
    Look at the pass_check() method. It has code to extract Passenger objects from an arraylist.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Reading from a text file into an ArrayList
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 17th, 2013, 01:24 AM
  2. 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
  3. 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
  4. 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
  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