Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    soxfan714 is offline Member
    Join Date
    Nov 2008
    Posts
    17
    Rep Power
    0

    Default [SOLVED] out of bounds exception help

    I am having problems with an out of bounds exception. The error message I get shows me where the problem should be, but I don't see why I am getting the error. Any help would be greatly appreciated.

    Here is the code so far:
    Java Code:
    	
    
    	private  void move()
    	{
    		
    		if (currentDirection == UP || currentFloor ==BOTTOM_FLOOR && currentFloor != TOP_FLOOR)
    		{
    		System.out.println ("Currently " +currentPassengers +" passengers onboard\n" +
    	    		  "Current Floor: " + currentFloor + "\nCurrently going up."); 
    		}
    		if (currentDirection == DOWN || currentFloor == TOP_FLOOR && currentFloor != BOTTOM_FLOOR)
    		{
    		System.out.println ("Currently " +currentPassengers +" passengers onboard\n" +
    		    		  "Current Floor: " + currentFloor + "\nCurrently going down."); 
    		}
    		
    	
    			
    		
    		
    		 if (currentDirection==UP)
    		 {
    			 if(currentFloor==TOP_FLOOR)
    			 {
    				 currentDirection=DOWN;
    			 }
    			 else
    			 {
    				 currentFloor++;
    			 }
    		 }// end currentDirection UP scenario
    		
    		 if (currentDirection==DOWN)
    		 {
    			 if (currentFloor==BOTTOM_FLOOR)
    			 {
    				 currentDirection=UP;
    				 
    			 }
    			 else
    			 {
    				 currentFloor--;
    			 }
    		
    			 
    		 
    		 }
    		
    			for (int s=0; s<=FLOORS; s++)// for loop to determine if elevator should stop
    			{
    				if (currentFloor==s && (destinationRequests[s][0]==1 || myFloors[s].waitingPassengers >0)  )
    				{
    					stop(s);
    				}
    				
    		      } 
    			 
    		 
    	}// end move method
    	
    
    	
    	public static void main (String [] args)
    	{
    	
    	
    		
    		
        Elevator myElevator= new Elevator();	
    		
        for (int i =0; i <=FLOORS-1; i++)
        {
        	myFloors[i]= new Floor(0);
        }
        //for (int i=0; i <= MAX_ELEVATOR_RUNS; i++){
    	myElevator.boardPassenger(2);
    	myElevator.boardPassenger(2);
    	myElevator.boardPassenger(3);
    	
    	//set waiting passengers
    	myFloors [2].waitingPassengers = 3;
    	myFloors [3].waitingPassengers = 2;
    	myFloors [4].waitingPassengers = 1;
    	myFloors [6].waitingPassengers = 6;
    	myElevator.registerRequest();
    	
    	
    	
        
    	for (int i =0; i <=MAX_ELEVATOR_RUNS; i++){
    	
         myElevator.move();
    	
        }	
    	
    	
    		
    	}//end main method
    The error message I am getting is :
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
    at cscie160.hw2.Elevator.move(Elevator.java:124)
    at cscie160.hw2.Elevator.main(Elevator.java:240)
    Last edited by soxfan714; 11-11-2008 at 03:40 AM.

  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

    Please use code tags and format your code when you posting again. Unformatted codes are not easy to read.

    Can you post only the code segment where you get that error. And also can you post full error message? Reading such a long code segment get board.

  3. #3
    soxfan714 is offline Member
    Join Date
    Nov 2008
    Posts
    17
    Rep Power
    0

    Default Edited

    Thanks, I think I have made the changes you requested

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

  5. #5
    soxfan714 is offline Member
    Join Date
    Nov 2008
    Posts
    17
    Rep Power
    0

    Default

    possibly this

    Java Code:
    for (int i =0; i <=FLOORS-1; i++)
        {
        	myFloors[i]= new Floor(0);
        }
    I am not sure on the line numbers, I can only figure out where the errors are for this part of the error message
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
    at cscie160.hw2.Elevator.move(Elevator.java:124)
    Java Code:
    				if (currentFloor==s && (destinationRequests[s][0]==1 || myFloors[s].waitingPassengers >0)  )
    at cscie160.hw2.Elevator.main(Elevator.java:240)
    Java Code:
         myElevator.move();

  6. #6
    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

    I think it here,

    Java Code:
    myFloors[i]= new Floor(0);
    What's the size of myFloors and the value of FLOORS?

  7. #7
    soxfan714 is offline Member
    Join Date
    Nov 2008
    Posts
    17
    Rep Power
    0

    Default

    the value of FLOORS is 7.

    myFloors is size 6

  8. #8
    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

    So it's wrong. At the last time, you get that i = 6.

    Java Code:
    for (int i =0; i <=FLOORS-1; i++)
    Is that clear?

    Then,

    Java Code:
    myFloors[6]= new Floor(0);
    In this case you get OutOfBoundsException.

    Reason is, arrays are zero indexing. That means to refer the first element you must use myFloors[0]. So the index is less than one of the array size. So in your array, maximum valid index is 5, hence myFloors[5] is the 6th element.

  9. #9
    soxfan714 is offline Member
    Join Date
    Nov 2008
    Posts
    17
    Rep Power
    0

    Default

    Even if I change the array size, say to
    Java Code:
     for (int i =0; i <=FLOORS +1; i++)
        {
        	myFloors[i]= new Floor(0);
        }
    and i get this error:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
    at cscie160.hw2.Elevator.main(Elevator.java:222)
    (myFloors(i) = newFloors(0))

  10. #10
    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

    Just think about this lol.

    Java Code:
    i <=FLOORS +1
    What is the maximum value that i can take.

    Since FLOORS = 7 maximum value for i is 8. That index is invalid for array. Try something like this,

    Java Code:
    i < FLOORS -1

  11. #11
    soxfan714 is offline Member
    Join Date
    Nov 2008
    Posts
    17
    Rep Power
    0

    Default

    I believe that is what I had before (see previous post) and still had errors

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

  13. #13
    soxfan714 is offline Member
    Join Date
    Nov 2008
    Posts
    17
    Rep Power
    0

    Default

    tried with this code:

    Java Code:
    Elevator myElevator= new Elevator();	
    		
        for (int i =0; i < FLOORS -1; i++)
        {
        	myFloors[i]= new Floor(0);
        }
        //for (int i=0; i <= MAX_ELEVATOR_RUNS; i++){
    	myElevator.boardPassenger(2);
    	myElevator.boardPassenger(2);
    	myElevator.boardPassenger(3);
    	
    	//set waiting passengers
    	myFloors [2].waitingPassengers = 3;
    	myFloors [3].waitingPassengers = 2;
    	myFloors [4].waitingPassengers = 1;
    	myFloors [6].waitingPassengers = 6;
    and still got this error:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
    at cscie160.hw2.Elevator.move(Elevator.java:124)
    at cscie160.hw2.Elevator.main(Elevator.java:241)
    (same as before)

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

  15. #15
    soxfan714 is offline Member
    Join Date
    Nov 2008
    Posts
    17
    Rep Power
    0

    Default

    Line 7 of my code is blank. It is the blank space between the package declaration and the class name.

    Line 7 of main is
    Java Code:
    for (int i =0; i < FLOORS -1; i++)

  16. #16
    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

    Java Code:
    for (int i =0; i < FLOORS -1; i++)
    You say that still this gives the error? Can you post the complete class here.

  17. #17
    soxfan714 is offline Member
    Join Date
    Nov 2008
    Posts
    17
    Rep Power
    0

    Default

    Here is the complete class:

    Java Code:
    package Elevator;
    
    
    
    
    public class Elevator {
    
    	//constant values
    	final int MAX_CAPACITY=10;
    	final static int FLOORS = 7;
    	final static int TOP_FLOOR = FLOORS;
    	final static int BOTTOM_FLOOR = 1;
    	final static int UP = 1;
    	final static int DOWN = 0;
    	final static int MAX_ELEVATOR_RUNS =5;
    	
    	//variables
    	static int currentFloor=1;
    	static int currentPassengers=0;
    	static int currentDirection=UP;
    	static int [][] destinationRequests= new int [FLOORS][2];
    	//int destinationFloor;
    	//int destinationRequest[] = new int[FLOORS];
    	static Floor [] myFloors = new Floor [FLOORS];
    	public boolean [] requestWaitingFloor = new boolean [FLOORS+1];
    
    
    	//constructor
    	Elevator ()
    	{
    	
    	currentFloor=1;
    	currentPassengers=0;
    	currentDirection=UP;
    	destinationRequests = new int[FLOORS +1][2];
    	requestWaitingFloor = new boolean [FLOORS];
    	}//end elevator constructor
    	
    	//to string method
    
    	public  String toString() 
    	{
    	      return( "Currently " +currentPassengers +" passengers onboard\n" +
    	    		  "Current Floor: " + currentFloor);
    	      
    	}// end toString method
    	
    	//move method
    
    	private  void move()
    	{
    		
    		if (currentDirection == UP || currentFloor ==BOTTOM_FLOOR && currentFloor != TOP_FLOOR)
    		{
    		System.out.println ("Currently " +currentPassengers +" passengers onboard\n" +
    	    		  "Current Floor: " + currentFloor + "\nCurrently going up."); 
    		}
    		if (currentDirection == DOWN || currentFloor == TOP_FLOOR && currentFloor != BOTTOM_FLOOR)
    		{
    		System.out.println ("Currently " +currentPassengers +" passengers onboard\n" +
    		    		  "Current Floor: " + currentFloor + "\nCurrently going down."); 
    		}
    		
    	
    			
    		
    		
    		 if (currentDirection==UP)
    		 {
    			 if(currentFloor==TOP_FLOOR)
    			 {
    				 currentDirection=DOWN;
    			 }
    			 else
    			 {
    				 currentFloor++;
    			 }
    		 }// end currentDirection UP scenario
    		
    		 if (currentDirection==DOWN)
    		 {
    			 if (currentFloor==BOTTOM_FLOOR)
    			 {
    				 currentDirection=UP;
    				 
    			 }
    			 else
    			 {
    				 currentFloor--;
    			 }
    		
    			 
    		 
    		 }
    		
    			for (int s=0; s<=FLOORS; s++)// for loop to determine if elevator should stop
    			{
    				if (currentFloor==s && (destinationRequests[s][0]==1 || requestWaitingFloor[s]==true)  )
    				{
    					stop(s);
    				}
    				
    		      } 
    			 
    		 
    	}// end move method
    	
    	//boardPassengers method
    	
    	public void boardPassenger(int floor){
    	
    	  //floor = floor -1;
    	  destinationRequests [floor][0]=1;
    	  destinationRequests [floor][1]++;
    	  
    	  currentPassengers++;
    		  
    		  
    	  
    	  
    	  
    	   
    	}// end board passenger
    	
    	//stop method
    	
    	private static void stop(int s)
    	{
    		System.out.println("\nStoping on floor "+ currentFloor);
    		
    		
    		myFloors[s].unloadPassengers(s);
    		
    		//System.out.println( "Currently " +currentPassengers +" passengers onboard\n" +
    	    		 // "Current Floor: " + currentFloor);
    		
    		
    		
    		
    		
    	}// end stop method
    	
    	//registerRequest method
    	public void registerRequest()
    	{
    		
    	if (myFloors[currentFloor].waitingPassengers >0)
    	{
    		requestWaitingFloor[currentFloor]= true;
    		System.out.println("This is working");
    		
    	}
    	else
    	{
    		requestWaitingFloor[currentFloor] = false;
    	}
    	}// end registerRequest method
    	
    	
    	
    	public static void main (String [] args)
    	{
    	
    	
    		
    		
        Elevator myElevator= new Elevator();	
    		
        for (int i =0; i < FLOORS - 1; i++)
        {
        	myFloors[i]= new Floor(0);
        }
        //for (int i=0; i <= MAX_ELEVATOR_RUNS; i++){
    	myElevator.boardPassenger(2);
    	myElevator.boardPassenger(2);
    	myElevator.boardPassenger(3);
    	
    	//set waiting passengers
    	myFloors [2].waitingPassengers = 3;
    	myFloors [3].waitingPassengers = 2;
    	myFloors [4].waitingPassengers = 1;
    	myFloors [5].waitingPassengers = 6;
    	myElevator.registerRequest();
    	
    	
    	
        
    	for (int i =0; i <=MAX_ELEVATOR_RUNS; i++){
    	
         myElevator.move();
    	
        }	
    	
    	
    		
    	}//end main method
    }// end Elevator

  18. #18
    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

    Can you tell me what are you trying to do here?

    Java Code:
    myFloors[i]= new Floor(0);
    Send the Floor class also. Is that only two class you are working on?

  19. #19
    soxfan714 is offline Member
    Join Date
    Nov 2008
    Posts
    17
    Rep Power
    0

    Default

    Yes, I am only working on the Elevator Class and the Floor Class.

    Here is the Floor Class:

    Java Code:
    package cscie160.hw2;
    
    public class Floor {
    
    	public int waitingPassengers;
    	private Elevator newElevator;
    	int floorRequested;
    	int passengersUnloading;
    	
    	
    	public Floor(int passengers)
    	{
    		newElevator = new Elevator ();
    		waitingPassengers = passengers;
    	}// end method Floor
    
    
    
    	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;
    		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");
    	}
    	}
    I am trying to create an array of floors to put passenger requests into for those waiting on a particular floor.
    myFloors [2].waitingPassengers = 3; for example should load a request to the second floor that 3 passengers are waiting to board.

    Therefore myFloors array size should hold up to floor 7.

  20. #20
    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

    Sorry for the misguide there. it's my fault.

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
    As I explain that out of index is 7. Index 7 cannot access there. It's fine. The error described in next line.

    at cscie160.hw2.Elevator.move(Elevator.java:124)
    Following is the incorrect access in your application, in Elevator class.

    Java Code:
    for (int s=0; s<=FLOORS; s++)
    Just remove the = sign and see

Page 1 of 2 12 LastLast

Similar Threads

  1. Help with Exception
    By bozovilla in forum New To Java
    Replies: 2
    Last Post: 10-19-2008, 05:19 AM
  2. Exception!
    By rameshraj in forum Advanced Java
    Replies: 1
    Last Post: 05-05-2008, 01:39 PM
  3. Where does the exception go?
    By aytidaalkuhs in forum New To Java
    Replies: 3
    Last Post: 04-07-2008, 02:24 PM
  4. why is my array out of bounds?
    By Phobos0001 in forum New To Java
    Replies: 3
    Last Post: 03-24-2008, 01:20 AM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 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
  •