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 a text file into an arraylist of fueldispensers.

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Reading a text file into an arraylist of fueldispensers.

    [highlight = java]
    public class FuelPump
    {
    //Text file

    private boolean poweredOn; // 1 premium 2.34 100
    private FuelDispenser activeDispenser; //2 regular 2.10 100
    private double currentSalePrice; // 3 midgrade 2.78 100
    private int gallonsPumped; // 4 diesel 2,78 100
    private ArrayList<FuelDispenser> dispensers;
    private Payment payment;


    public FuelPump() throws Exception
    {
    dispensers = new ArrayList<FuelDispenser>();
    Scanner input = new Scanner(new File("FuelDispenser_config.txt"));
    int IndexValue ;
    String FuelType ;
    double Price ;
    int Amount ;
    while(input.hasNext())
    {
    IndexValue = input.nextInt();
    FuelType = input.next();
    Price = input.nextDouble();
    Amount = input.nextInt();
    dispensers.add(new FuelDispenser(IndexValue, FuelType, Price, Amount));

    }
    input.close();

    currentSalePrice = 0.0;
    gallonsPumped = 0;
    payment = new Payment();
    turnOff();

    }

    public void welcome()
    {
    System.out.println("Welcome to CSE Gas Station");

    }

    public void askForPaymentType()
    {
    int response = 0;


    System.out.println("Select Payment Method");

    do{
    System.out.println("1. Credit");
    System.out.println("2. Cash");
    System.out.println("3. Shutdown/Exit Processing");

    Scanner sc = new Scanner(System.in);
    response = sc.nextInt();


    }while( response < 0 || response > 4 );
    if(response == 1)
    payment = new CreditCardPayment();
    if(response == 2)
    payment = new CashPayment();
    if(response == 3)
    System.exit(0);


    }

    public void askForFuelType()
    { int input = 0;
    int j = 0; double b = 0.0; String s = "";

    Scanner sc = new Scanner(System.in);

    System.out.println("Select Fuel Type:");
    do{
    for(int i = 0; i < dispensers.size(); i++)
    {
    j = dispensers.get(i).getFuelIndex();
    System.out.print(j);
    System.out.print(".");
    s = dispensers.get(i).getGrade();

    System.out.print(s);
    System.out.print(" :");
    b = dispensers.get(i).getPrice();

    System.out.println(b);



    }
    input = sc.nextInt();



    }while(input < 1 || input > 4);

    activeDispenser = dispensers.get(input);



    }

    public void processPayment()
    {
    payment.processPayment();
    }

    public void pump()
    {
    int gallonsPumped;
    double d = payment.getAmount();
    double b = activeDispenser.getPrice();


    int gallonsRequested = 0;
    if(poweredOn == true && b != 0.0 && d != 0.0)
    gallonsRequested = (int)(payment.getAmount()/b);

    gallonsPumped = activeDispenser.pumpFuel(gallonsRequested);
    currentSalePrice = gallonsPumped * activeDispenser.getPrice();
    payment.setAmount(currentSalePrice);




    }

    public void printReceipt()
    {
    payment.printReceipt(activeDispenser.getGrade(), gallonsPumped, activeDispenser.getPrice());




    }

    public void thankYouForYourBusiness()
    {
    System.out.println("Thank you for your business");

    }
    public void turnOn()
    {
    poweredOn = true;
    }

    public void turnOff()
    {
    poweredOn = false;
    }

    public String toString()
    {
    return "hello";

    }
    }
    [/highlight]


  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 a text file into an arraylist of fueldispensers.

    Please edit your post and wrap the code in code tags to preserve its formatting.

    Do you have a problem or question about your code?
    Please post it.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading a text file into an arraylist of fueldispensers.

    I am reading a text file FuelDispenser_config.txt . it contains data
    1 Premium 3.32 100
    2 Regular 3.76 100
    3 MidGrade 3.56 50
    4 Diesel 3.19 20
    After reading it into arraylist dipensers whem i try to output its contents
    output is
    0 null 0.0 0
    0 null 0.0 0
    0 null 0.0 0
    0 null 0.0 0
    How do i fix it?

  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 a text file into an arraylist of fueldispensers.

    You need to either edit your post and wrap code tags around the code
    or post a new version of your code with code tags.
    See: BB Code List - Java Programming Forums - The Java Community

    Check that the variables you are reading the data into are the same ones you are using when you print. It looks like there could be two different variables, one that gets the data and one that does not get any data.

    Where is the definition of the FuelDispenser class?
    Last edited by Norm; November 20th, 2011 at 01:26 PM.

Similar Threads

  1. Reading data from a text file into an object
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2011, 08:37 PM
  2. 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
  3. 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
  4. [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
  5. Reading from a text file. Help needed urgently.
    By TheAirPump in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 14th, 2009, 06:16 PM