Results 1 to 7 of 7
  1. #1
    allymoha is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Problem with file BufferedReader using binary file.

    Hi guys. The program simulates the Queue bank problem. It asks the user to input the number of servers/tellers and goes on to simulate the waiting time of Customers in a Queue in a bank. It computes the average waiting time. Apart from that, it also reads data from a binary file (Bank.Dat): the data includes arrival time for the customer and total service time for each customer. The problem lies in reading the file ......... Your assistance is needed. Thanks.

    Here is the entire program:

    Java Code:
    // This program is a simulation of a bank.  Customers arrive and wait in a queue
    // for an available teller.  They stay at the teller window for a prescribed
    // amount of time and then depart the bank.  Data for the program lists for each
    // arriving customer their arrival time and the amount of time they spend at the
    // teller window.  The program periodically prints a report of the simulation
    // status and calculates the average amount of time customes spend in the queue.
    // The user is prompted to enter the number of tellers used in the simulation.
    
    import javax.swing.*;
    import java.io.*;
    
    class Customer
    {
      int customerNumber;
      int tellerTime;
      int arrivalTime;
      Customer link;
    
      static int numberOfCustomers = 0;
      final int minutesPerHour = 60;
    
      Customer (String line)
      // This constructor inisitalizes a customer with the information from a data line.
      {
        customerNumber = ++numberOfCustomers;
        arrivalTime = Integer.parseInt(line.substring(0,1)) * minutesPerHour +
           Integer.parseInt(line.substring(2,4));
        tellerTime = Integer.parseInt(line.substring(4,8).trim());
        link = null;
      }
    }
    
    class Queue
    {
    	Customer front,rear;
    
    	Queue() { front = rear = null; }
    
    	void insert (Customer cus)
    	{
    		if (empty())
    		  front = cus;
    		else
    		  rear.link = cus;
    		rear = cus;
    	}
    
    	Customer remove ()
    	{
    		Customer cus = front;
    		front = front.link;
    		return cus;
    	}
    
    	boolean empty ()
    	{ return front == null; }
    
    	void display ()
    	{
    		System.out.print ("WAIT QUEUE : ");
    		Customer cus = front;
    		while (cus != null)
    		{
    		  System.out.print (cus.customerNumber + " ");
    		  cus = cus.link;
    	    }
    		System.out.println();
    	}
    }
    
    class Teller
    {
    	int tellerNumber;
    	int departureTime;
    	Customer customer;
    }
    
    class BankSimulation
    {
    	Teller teller[];
    	Customer nextCustomer;
    	BufferedReader bankData;
    	Queue waitQueue = new Queue();
    	int totalWaitTime = 0, numberOfDepartures = 0;
        final int simulationLength = 8 * 60;
        final int printTime = 60;
        int time;
    
    	public static void main (String args[])
    	{ (new BankSimulation()).runSimulation(); }
    
    	void runSimulation ()
    	// This method initializes the data file and tellers.  Then it runs the
    	// the simulation by polling the wait queue and each teller at each click
    	// of the clock to see if an event occurs at that time.  Finally, a report
    	// is generated.
    	{
    		try {bankData = new BufferedReader (new FileReader ("Bank.Dat")); }
    		catch (IOException ioex)
    		{ System.out.println ("ERROR OPENING THE FILE");
    		  System.exit(1);
    	    }
    
    	    getNextCustomer();
    	    int numberOfTellers = Integer.parseInt (
    			 JOptionPane.showInputDialog ("Enter the number of tellers: ").trim());
    	    teller = new Teller[numberOfTellers];
    	    for (int tell = 0; tell < numberOfTellers; tell++)
    	    {
    			teller[tell] = new Teller();
    			teller[tell].tellerNumber = tell + 1;
    			teller[tell].customer = null;
    		}
    
            for (time = 0; time <= simulationLength; time++)
            {
    			for (int tell = 0; tell < numberOfTellers; tell++ )
    			  if (teller[tell].customer != null &&
    			                teller[tell].departureTime == time)
    			    processDeparture (tell);
    
    			while (nextCustomer != null && nextCustomer.arrivalTime == time)
    			  processArrival();
    
    			if (time % printTime == 0 && time > 0)
    			{
    				System.out.println ("\n\nSIMULATION REPORT AT TIME " +
    				     (time / 60) + ":" + (time % 60 / 10) + (time % 10));
    				waitQueue.display();
    				for (int tell = 0; tell < numberOfTellers; tell++)
    				{
    				  System.out.print ("TELLER " + teller[tell].tellerNumber);
    				  if (teller[tell].customer != null)
    				    System.out.print ("  "+teller[tell].customer.customerNumber);
    				  System.out.println ();
    			    }
    			}
    		}
    
    		System.out.println ("\n\nSIMULATION SUMMARY");
    		System.out.println ("NUMBER OF DEPARTURES: " + numberOfDepartures);
    		System.out.println ("MEAN WAIT TIME: " +
    		         (((float)totalWaitTime) / numberOfDepartures));
    	}
    
    	void getNextCustomer()
    	// This method reads one line of the data file, creates one customer object,
    	// and saves that object as nextCustomer.
    	{
    		String line = null;
    		try {line = bankData.readLine();}
    		catch (IOException ioex)
    		{System.out.println ("Reading Error"); System.exit(1);}
    
    		if (line == null)
    		  nextCustomer = null;
    		else
    		  nextCustomer = new Customer (line);
    	}
    
    	void processArrival()
    	// This method is called when an arrival event occurs.  It checks for an
    	// available teller.  If one is found, the customer is placed at that
    	// teller window. Otherwise, the customer is placed in the wait queue.
    	// Then the next customer is generated.
    	{
    		int tell = 0;
    		while (tell < teller.length && teller[tell].customer != null)
    		  tell++;
    		if (tell == teller.length)
    		  waitQueue.insert (nextCustomer);
    		else
    		{
    		  teller[tell].customer = nextCustomer;
    		  teller[tell].departureTime = time + nextCustomer.tellerTime;
    	    }
    
    	   getNextCustomer();
    	}
    
    	void processDeparture (int tell)
    	// This method is called when a departure event occurs.  It records
    	// statistics from the departing customer.  If the wait queue is not
    	// empty, the front customer on the wait queue is placed at the teller
    	// window.  The parameter tells which teller has the departure.
    	{
    		numberOfDepartures++;
    		totalWaitTime += teller[tell].departureTime -
    		                 teller[tell].customer.tellerTime -
    		                 teller[tell].customer.arrivalTime;
    
    		if (waitQueue.empty())
    		   teller[tell].customer = null;
    		else
    		{
    			teller[tell].customer = waitQueue.remove();
    			teller[tell].departureTime += teller[tell].customer.tellerTime;
    		}
    	}
    }
    Last edited by pbrockway2; 12-16-2011 at 09:06 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Problem with file BufferedReader using binary file.

    Hi, welcome to the forum!

    When you post code, use the "code" tags. You put [code] at the start of the code and [/code] at the end. That way the formatting gets preserved when the code appears here.

    -----

    Your assistance is needed
    Perhaps it would be a good idea to say what the problem is.

    Does the program compile? If not, and you cannot understand what the compiler is complaining about, post the compiler's output and someone is sure to explain what it means.

    Does the program not do, at runtime, what you expect or intend? In that case describe what the runtime behaviour actually is (including the full text of any exceptions), and what you were expecting or intending.

    -----

    One thing does strike my eye:

    Java Code:
    try {bankData = new BufferedReader (new FileReader ("Bank.Dat")); }
    		catch (IOException ioex)
    		{ System.out.println ("ERROR OPENING THE FILE");
    		  System.exit(1);
    	    }
    Print as much information as possible when an exception occurs.

    Java Code:
    try 
    {
        bankData = new BufferedReader (new FileReader ("Bank.Dat")); 
    }
    catch (IOException ioex)
    { 
        System.err.println ("ERROR OPENING THE FILE");
        ioex.printStackTrace(); // <-- this is valuable information
        System.exit(1);
    }

  3. #3
    allymoha is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Problem with file BufferedReader using binary file.

    Hi pbrockway2,

    I added the printStackTrace(). The compile compiles as follows:

    Java Code:
    run:
    ERROR OPENING THE FILE
    java.io.FileNotFoundException: Bank.txt (The system cannot find the file specified)
    	at java.io.FileInputStream.open(Native Method)
    	at java.io.FileInputStream.<init>(FileInputStream.java:138)
    	at java.io.FileInputStream.<init>(FileInputStream.java:97)
    	at java.io.FileReader.<init>(FileReader.java:58)
    	at banksimulation2.BankSimulation2.runSimulation(BankSimulation2.java:35)
    	at banksimulation2.BankSimulation2.main(BankSimulation2.java:28)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

  4. #4
    allymoha is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Problem with file BufferedReader using binary file.

    The problem occurs when the program tries to read the file ("Bank.Dat").....

  5. #5
    allymoha is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Problem with file BufferedReader using binary file.

    Thanks @pbrockway2,

    I figure it out. Appreciate your since effort. You guys are doing a great job.

    Regards.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Problem with file BufferedReader using binary file.

    Java Code:
    java.io.FileNotFoundException: Bank.txt (The system cannot find the file specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:138)
        at java.io.FileInputStream.<init>(FileInputStream.java:97)
        at java.io.FileReader.<init>(FileReader.java:58)
        at banksimulation2.BankSimulation2.runSimulation(BankSimulation2.java:35)
    This exception pretty much means what it says: at runtime the program cannot find the file. You read the stack trace from the top downwards until you reach a line of your code. (Here line 35). It is that point that the problem arises.

    First, notice that the code that gave rise to this exception is not the same as the code you posted (different class). In any case it is often a good idea when a file cannot be found, to print the full path to the file. This allows you to check that your program is looking for the file in the same place that you put it.

    Something like:

    Java Code:
    try 
    {
        System.out.println("About to read file " + new File("Bank.Dat").getAbsolutePath());
        bankData = new BufferedReader (new FileReader ("Bank.Dat")); 
    }
    catch (IOException ioex)
    {
        // etc

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Problem with file BufferedReader using binary file.

    Too slow :(

    Glad you've got it figured out!

Similar Threads

  1. Image file to binary file conversion
    By Mantra in forum New To Java
    Replies: 5
    Last Post: 11-29-2010, 01:59 PM
  2. update binary file from text file
    By billdef in forum New To Java
    Replies: 8
    Last Post: 09-02-2010, 10:24 AM
  3. Replies: 3
    Last Post: 05-10-2009, 12:31 AM
  4. Reading a file from Applet (BufferedReader)
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 06-22-2008, 11:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •