Im writing a garage java program and im getting out of bound

I\'m writing a garage java program and im getting out of bounds exceptions, my temp move method is messed up, and im having problems print the file to a txt.file. any help would be great.

Summary

The garage is a single row of 10 (0-9). Cars can only leave from position 0. all cars in front of the car trying to leave have to be temporary moved and accounted for. And we are not alowed to use array list.

Current code

Car Class

public class car
{
  
private String licensePlate;
private int timesMoved = 0;
  
/**
*
* @param plate stores the value plate
*/
public car(String plate)
{
licensePlate = plate;
}
  
/**
* adds the times a car was temporary moved
*/
public void carMoves()
{
timesMoved++;
}
  
/**
*
* @return the times a car was moved
*/
public int getCarMoves()
{
return timesMoved;   
}
  
/**
*
* @return license plate stored
*/
public String getLicensePlate()
{
return licensePlate ;
}
}

import java.util.Arrays;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;

Garage class

public class garage
{

private car[] garageCars;
private String action;
private String plate;
private final int MAX_PARKING = 10;
private int carCounter = 0; // counts the cars in the garage
private int timesMoved = 0;
  
/**
* constructing an object for cars
*/
public garage()
{
garageCars = new car[MAX_PARKING];
}

/**
* this method reads the garage txt file
* and splits the string into two with
* tokenizer
* @param fileName is the garage.txt file
* @throws FileNotFoundException
*/
public void readData(String fileName) throws FileNotFoundException
{
StringTokenizer st = new StringTokenizer(fileName);
plate = st.nextToken();
action = st.nextToken();
System.out.println(\"Request: \" + plate + \" \" + action + \"\ \ \");
}
  
/**
* a method for cars arriving, if garage contains 10 cars then car is turned
* away. If not, a new car object is made incrementing by 1.
* @return returns the results of arrival.
*/
public String arrive()
{
String arrive;
if (carCounter >= MAX_PARKING)
{
arrive = \"Sorry, \" + plate + \" was turned away due to insufficient parking space. \ \ \";
return arrive;
}
else
{
garageCars[carCounter] = new car(plate);
arrive = \"Good news, \" + plate + \" was parked in position \" + carCounter + \". \ \ \";
carCounter++;
}
return arrive;
}

/**
* if the car being requested to depart exist in the parking lot it leaves.
* all cars behind the car requested shift down one. all cars in front
* temporary move out so the requested car may leave.
* @return
*/
public String depart()
{
boolean carInLot = false;
String carAction = \"\";
int carPosition; // element containing car object
carPosition = 0;
  
for (int i = 0; i < carCounter; i++) // loop checking if there are any cars parked
{
if (garageCars[i].getLicensePlate().equals(plate))
{
carInLot = true;
carPosition = i;
break;
}
  
}

if (!carInLot)
{
carAction = \"We\'re sorry \" + plate + \", has not parked in this garage \ \ \";
}
  
else if (carInLot)
{
if (carPosition == 0) // if car position is 0 everything behind it moves down 1
{
  
for (int i = carCounter; i > 0; i-- )
{
int j = carCounter;
garageCars[j] = garageCars[j-1];

carAction = \"Vehicle \" + plate + \" has departed and was temporarly moved \" + timesMoved + \" times.\";
}   

carCounter--;
}
  
else if (carPosition > 0)
{
for (int i = carPosition + 1; i <= carCounter; i++) // counting the times a car was temp moved
{
garageCars[i].carMoves();
timesMoved = garageCars[i].getCarMoves();
carAction = \"Vehicle \" + plate + \" has departed and was temporarly moved \" + timesMoved + \" times.\";
}
for (int i = carCounter; i > 0; i-- ) // shift cars down 1
{
int j = carCounter;
garageCars[j] = garageCars[j-1];
}   
carCounter--;
  
}
  
}

return carAction;
}
  
/**
*
* @param output prints the output from depart and arrive methods in txt
* @throws IOException
*/
public void outToText(String output) throws IOException
{
FileWriter outputText = new FileWriter(\"MyGarageActions.txt\",true);
outputText.write(output);
outputText.close();
}
  
}

Tester


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.StringTokenizer;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author jmart790
*/
public class GarageTester {

/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException
{
garage myGarage = new garage();
URL garageText = new URL(\"http://users.cis.fiu.edu/~shawg/3337/garage.txt\");
BufferedReader in = new BufferedReader(new InputStreamReader(garageText.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
{
myGarage.readData(inputLine);
StringTokenizer st = new StringTokenizer(inputLine);
String plateNum = st.nextToken();
String carStatus = st.nextToken();
switch (carStatus) {
case \"ARRIVE\":
System.out.println(myGarage.arrive());
// myGarage.outToText(myGarage.arrive());
break;
case \"DEPART\":
System.out.println(myGarage.depart());
// myGarage.outToText(myGarage.depart());
break;
}
}
in.close();

  
  
  
  
}

}

Solution

Exact issue explanation: You are maintaning MAX_PARKING variable as 10, but in garageCars arrays also you are using index value as 10.

Array index start from 0 to 9 only for maintain 10 cars. But in your code you are trying to access garageCars [10], which cause ArrayOutOfBoundsException.

Issue in two places...

public String depart()
{
boolean carInLot = false;
String carAction = \"\";
int carPosition; // element containing car object
carPosition = 0;
  
for (int i = 0; i < carCounter; i++) // loop checking if there are any cars parked
{
if (garageCars[i].getLicensePlate().equals(plate))
{
carInLot = true;
carPosition = i;
break;
}
  
}

if (!carInLot)
{
carAction = \"We\'re sorry \" + plate + \", has not parked in this garage \ \ \";
}
  
else if (carInLot)
{
if (carPosition == 0) // if car position is 0 everything behind it moves down 1
{
  
for (int i = carCounter; i > 0; i-- )
{
int j = carCounter;
garageCars[j] = garageCars[j-1];

carAction = \"Vehicle \" + plate + \" has departed and was temporarly moved \" + timesMoved + \" times.\";
}   

carCounter--;
}
  
else if (carPosition > 0)
{
for (int i = carPosition + 1; i <= carCounter; i++) // counting the times a car was temp moved
{
// here carCounter was coming as 10 so issue there

garageCars[i].carMoves();
timesMoved = garageCars[i].getCarMoves();
carAction = \"Vehicle \" + plate + \" has departed and was temporarly moved \" + timesMoved + \" times.\";
}
for (int i = carCounter; i > 0; i-- ) // shift cars down 1
{
int j = carCounter; // here carCounter was coming as 10 so issue there
garageCars[j] = garageCars[j-1];
}   
carCounter--;
  
}
  
}

return carAction;
}

--------------------------------------------------------------------------------------------------------------------------------------------------------------

To fix this, we need to stop iteration explicitly when carCounter is 10. To implement this use \"if condition\". Here is the modified code (modiied statements I made bold) and output attached.

Only garage.java modified..

import java.util.Arrays;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;

public class garage {

private car[] garageCars;
private String action;
private String plate;
private final int MAX_PARKING = 10;
private int carCounter = 0; // counts the cars in the garage
private int timesMoved = 0;

/**
* constructing an object for cars
*/
public garage() {
garageCars = new car[MAX_PARKING];
}

/**
* this method reads the garage txt file and splits the string into two with
* tokenizer
*
* @param fileName is the garage.txt file
* @throws FileNotFoundException
*/
public void readData(String fileName) throws FileNotFoundException {
StringTokenizer st = new StringTokenizer(fileName);
plate = st.nextToken();
action = st.nextToken();
System.out.println(\"Request: \" + plate + \" \" + action + \"\ \ \");
}

/**
* a method for cars arriving, if garage contains 10 cars then car is turned
* away. If not, a new car object is made incrementing by 1.
*
* @return returns the results of arrival.
*/
public String arrive() {
String arrive;
if (carCounter >= MAX_PARKING) {
arrive = \"Sorry, \" + plate + \" was turned away due to insufficient parking space. \ \ \";
return arrive;
} else {
garageCars[carCounter] = new car(plate);
arrive = \"Good news, \" + plate + \" was parked in position \" + carCounter + \". \ \ \";
carCounter++;
}
return arrive;
}

/**
* if the car being requested to depart exist in the parking lot it leaves.
* all cars behind the car requested shift down one. all cars in front
* temporary move out so the requested car may leave.
*
* @return
*/
public String depart() {
boolean carInLot = false;
String carAction = \"\";
int carPosition; // element containing car object
carPosition = 0;

for (int i = 0; i < carCounter; i++) // loop checking if there are any cars parked
{
if (garageCars[i].getLicensePlate().equals(plate)) {
carInLot = true;
carPosition = i;
break;
}

}
if (!carInLot) {
carAction = \"We\'re sorry \" + plate + \", has not parked in this garage \ \ \";
} else if (carInLot) {
if (carPosition == 0) // if car position is 0 everything behind it moves down 1
{

for (int i = carCounter; i > 0; i--) {
int j = carCounter;
garageCars[j] = garageCars[j - 1];

carAction = \"Vehicle \" + plate + \" has departed and was temporarly moved \" + timesMoved + \" times.\";
}

carCounter--;
} else if (carPosition > 0) {
for (int i = carPosition + 1; i <= carCounter; i++) // counting the times a car was temp moved
{
if(i >= 10){ // if carcounter greater than 10, skip it
break;
}

garageCars[i].carMoves();
timesMoved = garageCars[i].getCarMoves();
carAction = \"Vehicle \" + plate + \" has departed and was temporarly moved \" + timesMoved + \" times.\";
}
for (int i = carCounter; i > 0; i--) // shift cars down 1
{
int j = carCounter;
if(j<10){ // if carcounter greater than 10, skip it
garageCars[j] = garageCars[j - 1];
}

}
carCounter--;

}

}
return carAction;
}

/**
*
* @param output prints the output from depart and arrive methods in txt
* @throws IOException
*/
public void outToText(String output) throws IOException {
FileWriter outputText = new FileWriter(\"MyGarageActions.txt\", true);
outputText.write(output);
outputText.close();
}

}

OUTPUT

run:
Request: JAV001 ARRIVE

Good news, JAV001 was parked in position 0.

Request: JAV002 ARRIVE

Good news, JAV002 was parked in position 1.

Request: JAV003 ARRIVE

Good news, JAV003 was parked in position 2.

Request: JAV004 ARRIVE

Good news, JAV004 was parked in position 3.

Request: JAV005 ARRIVE

Good news, JAV005 was parked in position 4.

Request: JAV001 DEPART

Vehicle JAV001 has departed and was temporarly moved 0 times.
Request: JAV004 DEPART

Vehicle JAV004 has departed and was temporarly moved 1 times.
Request: JAV006 ARRIVE

Good news, JAV006 was parked in position 3.

Request: JAV007 ARRIVE

Good news, JAV007 was parked in position 4.

Request: JAV008 ARRIVE

Good news, JAV008 was parked in position 5.

Request: JAV009 ARRIVE

Good news, JAV009 was parked in position 6.

Request: JAV010 ARRIVE

Good news, JAV010 was parked in position 7.

Request: JAV011 ARRIVE

Good news, JAV011 was parked in position 8.

Request: JAV012 ARRIVE

Good news, JAV012 was parked in position 9.

Request: JAV013 ARRIVE

Sorry, JAV013 was turned away due to insufficient parking space.

Request: JAV014 ARRIVE

Sorry, JAV014 was turned away due to insufficient parking space.

Request: JAV006 DEPART

Vehicle JAV006 has departed and was temporarly moved 1 times.
Request: JAV014 DEPART

We\'re sorry JAV014, has not parked in this garage

Request: JAV013 DEPART

We\'re sorry JAV013, has not parked in this garage

Request: JAV005 DEPART

We\'re sorry JAV005, has not parked in this garage

Request: JAV015 ARRIVE

Good news, JAV015 was parked in position 9.

Request: JAV010 DEPART

Vehicle JAV010 has departed and was temporarly moved 1 times.
Request: JAV002 DEPART

Vehicle JAV002 has departed and was temporarly moved 2 times.
Request: JAV015 DEPART

We\'re sorry JAV015, has not parked in this garage

Request: JAV013 DEPART

We\'re sorry JAV013, has not parked in this garage

Request: JAV009 DEPART

Vehicle JAV009 has departed and was temporarly moved 4 times.
Request: JAV003 DEPART

Vehicle JAV003 has departed and was temporarly moved 4 times.
Request: JAV008 DEPART

Vehicle JAV008 has departed and was temporarly moved 4 times.
Request: JAV007 DEPART

Vehicle JAV007 has departed and was temporarly moved 4 times.
Request: JAV012 DEPART

We\'re sorry JAV012, has not parked in this garage

Request: JAV011 DEPART

We\'re sorry JAV011, has not parked in this garage

BUILD SUCCESSFUL (total time: 1 second)

I\'m writing a garage java program and im getting out of bounds exceptions, my temp move method is messed up, and im having problems print the file to a txt.fil
I\'m writing a garage java program and im getting out of bounds exceptions, my temp move method is messed up, and im having problems print the file to a txt.fil
I\'m writing a garage java program and im getting out of bounds exceptions, my temp move method is messed up, and im having problems print the file to a txt.fil
I\'m writing a garage java program and im getting out of bounds exceptions, my temp move method is messed up, and im having problems print the file to a txt.fil
I\'m writing a garage java program and im getting out of bounds exceptions, my temp move method is messed up, and im having problems print the file to a txt.fil
I\'m writing a garage java program and im getting out of bounds exceptions, my temp move method is messed up, and im having problems print the file to a txt.fil
I\'m writing a garage java program and im getting out of bounds exceptions, my temp move method is messed up, and im having problems print the file to a txt.fil
I\'m writing a garage java program and im getting out of bounds exceptions, my temp move method is messed up, and im having problems print the file to a txt.fil
I\'m writing a garage java program and im getting out of bounds exceptions, my temp move method is messed up, and im having problems print the file to a txt.fil
I\'m writing a garage java program and im getting out of bounds exceptions, my temp move method is messed up, and im having problems print the file to a txt.fil

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site