Why does a method act differently when invoked manually to when invoked through the driver class?  
Author Message
Rowan B





PostPosted: 2004-12-10 12:38:00 Top

java-programmer, Why does a method act differently when invoked manually to when invoked through the driver class? Can anyone tell me why, when I run the driver class InstantiateFerry to read
a file containing the test just below, this part of the code:
ferry.getvehicleLocation(); produces the message Vehicle MVK806R is in lane
1 and position 1, which I don't want, but when I enter the file details
manually and invoke the method public void getvehicleLocation(), I get the
message I want with correct values for lane and position rather than just 1
and 1 every time?

BLUEJ_BELLE 3 120.0 285
# Test1.txt: Demonstration file for Project 2, November 2004
KR02123 1.04 5.1
DR02123 1.56 7.64
XR02123 1.049 5.1
L02123 3.15 17.1
YR02123 1.04 5.1
FR02123 1.56 7.65
BB51765 1.125 6.75
DB51765 1.6875 10.125
MVK806R 1.6875 10.125
FR02123 2.33 11.43
BR02123 1.049 5.1
MR02123 1.5735 7.66
SR02123 1.5735 7.63
CR02123 1.5735 7.64


public void addVehicle(Vehicle v)
{

int count = 0;
//System.out.println("deckLength "+deckLength+ " takenSpace
"+takenSpace+" getspaceTaken "+v.getspaceTaken());

if(deckLength < (takenSpace + v.getspaceTaken()))
{
laneCount += 1;
spaceLeft = takenSpace;
takenSpace = 0;
counter = 0;
}

//System.out.println("lanes " +lanes+ " laneCount " +laneCount);
if(laneCount == lanes)
{

System.out.println("Vehicle " + v.getReg() + " cannot board the
ferry as it is full.");

return;
}

while (deckArray[laneCount][count]!= null)
{
count += 1;

}

deckArray[laneCount][count] = v;
vehicleCount += 1;

for(int i = counter ; i < deckLength-1; i++)
{
if(deckArray[laneCount][i]!=null)
{
Vehicle vehicle = deckArray[laneCount][i];
takenSpace += vehicle.getspaceTaken();
}

}
counter += 1;

}

public void getvehicleLocation()
{
for(int lanePos = 0; lanePos < deckLength-1; lanePos++)
for(int laneLocation = 0; laneLocation < lanes; laneLocation++)
{
if(deckArray[laneLocation][lanePos]!=null)
{
Vehicle vehicle2 = deckArray[laneLocation][lanePos];
if (vehicle2.getReg() == "MVK806R")
{
vLane = laneLocation;
vPos = lanePos;
}
}
}
System.out.println("Vehicle MVK806R is in lane " + (vLane + 1) + "
and position " + (vPos + 1));
}

public int getlanePos()
{
return (vPos + 1);
}

public int getlaneLocation()
{
return (vLane + 1);
}
}

public class InstantiateFerry
{

private InstantiateFerry()
{
// Instances of this class are never created.
}

public static void main(String[] args)
{
// Here is an example statement showing you how to get at the name of
the

try
{
FileReader inFile = new FileReader(args[0]); // Open
the file
BufferedReader buffReader = new BufferedReader(inFile); // Turn
into BufferedReader
boolean endOfFile = false;
double totalCost = 0;

String line;

line = buffReader.readLine();
StringTokenizer st = new StringTokenizer(line);
st = new StringTokenizer(line, "\t");
String token = st.nextToken();
String token2 = st.nextToken();
int fLanes = Integer.parseInt(token2);
String token3 = st.nextToken();
double fLength = Double.parseDouble(token3);
String token4 = st.nextToken();
int fMiles = Integer.parseInt(token4);

Ferry ferry = new Ferry(token, fLanes, fLength, fMiles);
String line2;
line2 = buffReader.readLine();
System.out.println(line2);
double ferryLength = ferry.getLength();

//System.out.println(ferryLength);

do
{

String line3;

line3 = buffReader.readLine();
StringTokenizer st2 = new StringTokenizer(line3);
if (line3.equals(""))
{
endOfFile = true;
break;

}
else
{

while(st2.hasMoreTokens())
{ if (line3.equals(""))
{
endOfFile = true;
break;
}
else
{

String token5 = st2.nextToken();
String token6 = st2.nextToken();
double vWeight = Double.parseDouble(token6);
String token7 = st2.nextToken();
double vLength = Double.parseDouble(token7);
Vehicle vehicle = new Vehicle(token5, vWeight, vLength);
ferry.addVehicle(vehicle);
Money money = ferry.cost(vehicle);
totalCost += money.getCost();

//System.out.println(ferry.getSpace());

}
}

}
}
while (!endOfFile);
buffReader.close();
System.out.println(ferry.getferryName());
System.out.println("There are " + ferry.getVehicles() + " on the
ferry.");
System.out.println("There is " + ferry.getspaceLeft() + " metres
of space left on the ferry.");
System.out.println(totalCost);
ferry.getvehicleLocation();
// This method is supposed to find the location of Vehicle
"MVK806R".
}

catch (IOException e)
{
System.out.println("Caught unexpected exception " +
e.toString());
return;
}

} // End readFileBufferedReader()
} // End main()