Results 1 to 4 of 4
  1. #1
    soxfan714 is offline Member
    Join Date
    Nov 2008
    Posts
    17
    Rep Power
    0

    Default try catch exception

    I am new to java and writing a program for an elevator simulator. The floors are misreading now and I am not sure how to run the try catch exception to make the elevators continue to move. Any help would be greatly appreciated. I believe this is the relevant code.

    Java Code:
    public void boardPassenger(int floor){
    	
    	  floor = floor -1;
    	  try{
    	  destinationRequests [floor][0]=1;
    	  destinationRequests [floor][1]++;
    	  
    	  currentPassengers++;
    	  if (currentPassengers>=MAX_CAPACITY)
    	  {
    		  throw new ElevatorFullException ();
    	  }
    	  }
    	  catch (ElevatorFullException e)
    	  {
    		  System.out.println ("Elevator is at capacity");
    		  requestWaitingFloor[floor]=true;
    		  move();
    	  }
    It seems to still be calling this after calling the ElevatorFullException
    Java Code:
    public void unloadPassengers(int s) {
    		int floor = s ;
    		int passengers = newElevator.destinationRequests [floor][1];
    		newElevator.destinationRequests [floor][0]=0;
    		newElevator.destinationRequests [floor][1]=0;
    		newElevator.currentPassengers = newElevator.currentPassengers-passengers;
    		newElevator.printFloor=newElevator.currentFloor +1;
    		System.out.println( "Unloading " + passengers + " passengers\n" +
    				"Currently " +newElevator.currentPassengers +" passengers onboard\n" +
    	    		  "Current Floor: " + newElevator.currentFloor);
    		int waitingPassengers = newElevator.myFloors[floor].waitingPassengers;
    		
    		for (int i =0; i <waitingPassengers; i++)
    		{
    			newElevator.boardPassenger(1);
    		}
    				System.out.println("Boarding " + waitingPassengers + " waiting passengers." );
    						//"\nCurrenty " +newElevator.currentPassengers + " on board");
    				
    	}
    	}
    Java Code:
    public class ElevatorFullException extends Exception {
    	private Elevator myElevator;
    
    	public ElevatorFullException() 
    	{
    		myElevator = new Elevator ();
    	
    		
    			myElevator.requestWaitingFloor[myElevator.currentFloor]=true;
    		
    		
    	}
    	public String toString()
    	{
    		return "Elevator is at Maximum Capacity.  Passngers will be picked up later.";
    	}
    
    }
    This is the output I get:
    Currently 3 passengers onboard
    Current Floor: 7
    Currently going up. move up
    Currently 3 passengers onboard
    Current Floor: 7
    Currently going up. move up

    Stoping on floor 2
    Unloading 2 passengers
    Currently 1 passengers onboard
    Current Floor: 2
    Boarding 3 waiting passengers.
    Currently 4 passengers onboard
    Current Floor: 3
    Currently going up. move up

    Stoping on floor 3
    Unloading 1 passengers
    Currently 3 passengers onboard
    Current Floor: 3
    Boarding 2 waiting passengers.
    Currently 5 passengers onboard
    Current Floor: 4
    Currently going up. move up

    Stoping on floor 4
    Unloading 0 passengers
    Currently 5 passengers onboard
    Current Floor: 4
    Boarding 1 waiting passengers.
    Currently 6 passengers onboard
    Current Floor: 5
    Currently going up. move up
    Currently 6 passengers onboard
    Current Floor: 5
    Currently going up. move up

    Stoping on floor 6
    Unloading 0 passengers
    Currently 6 passengers onboard
    Current Floor: 6
    Elevator is at capacity
    Currently 0 passengers onboard
    Current Floor: 7
    Currently going up. move up

    Stoping on floor 1
    Unloading 0 passengers
    Currently 0 passengers onboard
    Current Floor: 1
    Boarding 0 waiting passengers.
    Boarding 6 waiting passengers.
    Currently 2 passengers onboard
    Current Floor: 2
    Currently going up. move up
    Currently 2 passengers onboard
    Current Floor: 2
    Currently going up. move up
    Currently 2 passengers onboard
    Current Floor: 2
    Currently going up. move up
    Currently 2 passengers onboard
    Current Floor: 2
    Currently going up. move up
    Currently 2 passengers onboard
    Current Floor: 2
    Currently going up. move up
    Currently 2 passengers onboard
    Current Floor: 2
    Currently going up. move up
    Currently 2 passengers onboard
    Current Floor: 2
    Currently going up. move up
    Currently 2 passengers onboard
    Current Floor: 6
    Currently going down. move down
    Currently 2 passengers onboard
    Current Floor: 5
    Currently going down. move down
    Currently 2 passengers onboard
    Current Floor: 4
    Currently going down. move down
    Currently 2 passengers onboard
    Current Floor: 3
    Currently going down. move down
    Currently 2 passengers onboard
    Current Floor: 2
    Currently going down. move down

    Stoping on floor 1
    Unloading 2 passengers
    Last edited by soxfan714; 11-11-2008 at 10:25 PM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by soxfan714 View Post
    I am new to java and writing a program for an elevator simulator. The floors are misreading now and I am not sure how to run the try catch exception to make the elevators continue to move. Any help would be greatly appreciated. I believe this is the relevant code.
    You cannot continue process using try-catch block. Try-catch doing is catch any exception in a piece of code. If you want to do something continuously you have to workout in a loop.

    What happen when you try your application?

  3. #3
    rifty50 is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default

    Is it possible for you to post the files?

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by rifty50 View Post
    Is it possible for you to post the files?
    Dude, can you see the date of last post? We are in 2011 :)

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 10:54 AM
  2. try catch!?
    By Joe2003 in forum Advanced Java
    Replies: 2
    Last Post: 01-28-2008, 07:51 PM
  3. Try Catch
    By Renegade85 in forum New To Java
    Replies: 4
    Last Post: 12-03-2007, 04:10 PM
  4. when to use try...catch
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-18-2007, 08:52 PM
  5. Use try and catch
    By zoe in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 07:50 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
  •