?
2010-04-07 07:36:00 UTC
A String variable, fullName, contains a name in one of two formats:
last name, first name (comma followed by a blank), or
first name last name (single blank)
Extract the first name into the String variable firstName and the last name into the String variable lastName. Assume the variables have been declared and fullName already initialized. You may also declare any other necessary variables.
Now here's the code I wrote:
int location1 = 0;
int location2 = 0;
location1 = fullName.indexOf(',');
location2 = fullName.indexOf(' ');
if( location1 > 0 )
{
lastName = fullName.substring(0, location1);
firstName = fullName.substring((location2 + 1), (fullName.length() - 1) );
}
else
{
firstName = fullName.substring(0,location2);
lastName = fullName.substring((location2 + 1 ), (fullName.length() - 1) );
}
Shouldn't this work? If someone could tell me what I'm doing wrong that would be greatly appreciated. This one has been giving me problems for quite some time -_-