iyea070
Jun 8, 2014, 06:31 AM
Hey everyone,
I have an issue where I have a method and I am trying to check if the name of an object is the same as a String parameter for the method. It does not seem to be working for me. If anyone could assist me, I would be grateful:
public String toString(String checkName)
{
int i=0;
while (i < count) //count is equal to 3
{
if (!vehicle[i].getName().equals(checkName)) //assume this method is in a 'Vehicle' class and the name for this object is being accessed through the getName() method
{
i++;
}
}
i--; //When running, the if statement is not working how I want it to work, so it continues incrementing 'i' till the value is '3', so this 'i--' decrements the value of 'i' to 2 so there isn't an ArrayIndexOutOfBoundsException exception.
return "Vehicle's name: "+vehicle[i].getName();
}
If it is not completely clear, I am trying to make it so that the if statement checks if that particular 'vehicle' has the same name to 'checkName', and if it does not, then the value of 'i' is incremented and the next 'vehicle' is checked, etc.
The returned String is:
Vehicle's name:
As you can see, the vehicle's name is being taken from the very last vehicle object, but the name has been set the first vehicle object.
I don't want to just do something like
vehicle[0].getName() because I need the method to check each vehicle object's name.
Thanks!
(Sorry if I haven't made it completely understandable)
I have an issue where I have a method and I am trying to check if the name of an object is the same as a String parameter for the method. It does not seem to be working for me. If anyone could assist me, I would be grateful:
public String toString(String checkName)
{
int i=0;
while (i < count) //count is equal to 3
{
if (!vehicle[i].getName().equals(checkName)) //assume this method is in a 'Vehicle' class and the name for this object is being accessed through the getName() method
{
i++;
}
}
i--; //When running, the if statement is not working how I want it to work, so it continues incrementing 'i' till the value is '3', so this 'i--' decrements the value of 'i' to 2 so there isn't an ArrayIndexOutOfBoundsException exception.
return "Vehicle's name: "+vehicle[i].getName();
}
If it is not completely clear, I am trying to make it so that the if statement checks if that particular 'vehicle' has the same name to 'checkName', and if it does not, then the value of 'i' is incremented and the next 'vehicle' is checked, etc.
The returned String is:
Vehicle's name:
As you can see, the vehicle's name is being taken from the very last vehicle object, but the name has been set the first vehicle object.
I don't want to just do something like
vehicle[0].getName() because I need the method to check each vehicle object's name.
Thanks!
(Sorry if I haven't made it completely understandable)