Reputation: 101
Write a method lastNameFirst that takes a string containing a name such as "Harry Smith" or "Mary Jane Lee", and that returns the string with the last name first, such as "Smith, Harry" or "Lee, Mary Jane".
im supposed to check it against http://wiley.code-check.org/codecheck/files?repo=bjlo2&problem=05_02
i post this
String firstName = name.substring(0, name.indexOf(" "));
String lastName = name.substring(name.indexOf(" "));
String cName = lastName + ", " + firstName;
if ( lastName == " " )
{
cName = firstName;
}
return cName;
I get 0/4 everytime please help im completely lost.
Upvotes: 0
Views: 4536
Reputation: 1
public class Names
{
/**
Changes a name so that the last name comes first.
@param name a name such as "Mary Jane Lee"
@return the reversed name, such as "Lee, Mary Jane".
If name has no spaces, it is returned without change.
*/
public static String lastNameFirst(String name)
{
String result = "";
if(!name.contains(" "))
{
String firstOnly = name.substring(0);
return firstOnly;
}
else
{
String first = name.substring(name.lastIndexOf(" ")+1);
String last = name.substring(0, name.lastIndexOf(" "));
result = first + ", " + last;
return result;
}
}
}
This is the correct answer that will get you 4/4.
Upvotes: 0
Reputation: 3
I tried this code and all the four arguement works. Hope this helps!!
{
String[] names = name.split(" ");
String cName = "";
if(names.length > 2){
cName = names[2].equals(" ") ? names[0] : names[2] + ", " + names[0] + " " + names[1];
}
else if(names.length == 1){
cName = names[0]
}
else{
cName = names[1].equals(" ") ? names[0] : names[1] + ", " + names[0];
}
return cName;
}
}
Upvotes: 0
Reputation: 1
I put this for that Question and it passed all 4 tests they had.
public static String lastNameFirst(String name) { String LastToFirst = "";
if(!name.contains(" ")){
return name;
}
int index = name.indexOf(" ");
int secondIndex = name.indexOf(" ", index + 1);
// exclusive of last index
if(secondIndex != -1) {
LastToFirst += name.substring(secondIndex+1,name.length())+", ";
LastToFirst += name.substring(0,secondIndex);
}
else {
LastToFirst += name.substring(index +1,name.length()) + ", ";
LastToFirst += name.substring(0, index);
return LastToFirst;
}
A better solution for this would be to use an array, and store the characters in there and for the spacing one should add an index variable for where you want the splitting to happen- the string of interest. The solutions above do a good example of expalining this better, they consider cases where it is not a white space, but other symbols making the method more robust. Hope this helps.
Upvotes: 0
Reputation: 201439
You only need the last space in your name
, which you can get with String.lastIndexOf(int)
. Then you test if that is less then 0
, if so return
the input name
. Otherwise, concatenate and return
your name
in the desired format. Using a ternary (or conditional operator ? :
) that might look something like,
int p = name.lastIndexOf(' ');
return (p < 0) ? name : name.substring(p + 1) + ", " + name.substring(0, p);
Upvotes: 0
Reputation: 357
Please try this code
String first = name.substring(name.lastIndexOf(" "));
String last = name.substring(0, name.lastIndexOf(" "));
String result = first + "," + last;
Upvotes: 1
Reputation: 4006
It might be simpler to create an array using the split
function of the String
class, then join them:
String cName = String.join(", ", Collections.reverse(Arrays.asList(name.split(" "))));
String.join
is only in Java 8 I believe, if you're not using 8 you can use something like the following:
String[] names = name.split(" ");
String cName = names[1] + ", " + names[0];
You should also be using the equals
method for comparing String and other objects:
String[] names = name.split(" ");
String cName = names[1].equals(" ") ? names[0] : names[1] + ", " + names[0];
Upvotes: 1
Reputation: 6274
Your solution is very close. Here's 2 hints to get to the right solution:
What separates the first and last name is the last, not the first space (consider using str.lastIndexOf(" ")
).
As mentioned in the comments, when comparing strings, you can't use str1 == str2
, you have to use str1.equals(str2)
Upvotes: 0