初学者 Java - 将字符串从 First Middle Last 更改为 Last, First Middle
Beginner Java - Changing String from First Middle Last to Last, First Middle
试图解决要求的家庭作业问题:
更改姓名,以便姓在前。
示例:"Mary Jane Lee" 将 return "Lee, Mary Jane"。
如果名称没有空格,则 returned 不变。
经过一些研究,我似乎可以用 Split 方法做到这一点,但我们还没有学会。
问题是我已经制定了代码,它似乎在空格时工作
并输入了全名,但是当没有中间名或没有空格分隔字符时,我收到错误消息:
当输入的名字只是:Harry Smith
java.lang.StringIndexOutOfBoundsException: String index out of range: -7
和
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
当名字叫莎拉时
这是我的代码,但我不确定如何修复它:
public class Names {
public static String lastNameFirst(String name) {
int firstIndex = name.indexOf(" ");
int secondIndex = name.indexOf(" ", firstIndex + 1);
String firstName = name.substring(0, name.indexOf(" "));
String middleName = name.substring(firstIndex + 1, secondIndex);
String lastName = name.substring(secondIndex + 1);
String result = "";
result = lastName + ", " + firstName + " " + middleName;
return result;
}
}
提前致谢!!
您的代码假定输入字符串至少包含两个空格。当该假设错误时(如输入 "Harry Smith" 和 "Sarah"),您会得到一个异常。
在使用它们的值之前,您必须检查 firstIndex
和 secondIndex
是否为正数。
使用 split 和 switch 会容易得多
String name = "Mary Jane Lee";
String arr[] = name.split (" ");
switch (arr.length) {
case 1:
System.out.println(name);
break;
case 2:
System.out.println(arr[1] + ", " + arr[0]);
break;
default:
System.out.println(arr[2] + ", " + arr[0] + " " + arr[1]);
}
更稳健的方法是使用lastIndexOf
找到最后一个space:
int lastSpace = name.lastIndexOf(' ');
if (lastSpace != -1) {
String lastName = name.substring(lastSpace + 1);
String partBeforeLastName = name.substring(0, lastSpace);
return lastName + ", " + partBeforeLastName;
} else {
return name;
}
您实际上并不真正关心另一个 space(如果它存在的话),因为名字和中间名保持相同的相对顺序。
(一般来说,有lots of falsehoods that programmers believe about names;但为了练习的目的,让我们把它们放在一边。)
问题是你的代码除了有 3 个名字。名字少时不处理
public static String lastNameFirst(String name)
{
int firstIndex = name.indexOf(" ");
if ( firstIndex >= 0 )
{
int secondIndex = name.indexOf(" ", firstIndex + 1 );
String firstName = name.substring(0, firstIndex);
if ( secondIndex >= 0 ) // we have 3 names
{
String middleName = name.substring(firstIndex + 1, secondIndex);
String lastName = name.substring(secondIndex + 1);
return lastName + ", " + firstName + " " + middleName;
}
else // we have 2 names
{
String lastName = name.substring(firstIndex + 1);
return lastName + ", " + firstName;
}
}
else // have only one name
return name;
}
也值得一试lastIndexOf()
:
public static String lastNameFirst(String name)
{
int lastIndex = name.lastIndexOf(" ");
if ( lastIndex >= 0 ) // have at least 2 names
{
String firstNames = name.substring(0,lastIndex);
String lastName = name.substring(lastIndex + 1);
return lastName + ", " + firstNames;
}
}
else // have only one name
return name;
}
此外,可以尝试不同的方法,将名称拆分为一个数组,如下所示:
public static String lastNameFirst(String name)
{
String[] parts = name.split(" ");
switch ( parts.length )
{
case 1:
return name;
case 2:
return parts[1] + ", " + parts[0];
case 3:
return parts[2] + ", " + parts[0] + " " + parts[1];
}
}
试图解决要求的家庭作业问题: 更改姓名,以便姓在前。 示例:"Mary Jane Lee" 将 return "Lee, Mary Jane"。 如果名称没有空格,则 returned 不变。
经过一些研究,我似乎可以用 Split 方法做到这一点,但我们还没有学会。
问题是我已经制定了代码,它似乎在空格时工作 并输入了全名,但是当没有中间名或没有空格分隔字符时,我收到错误消息:
当输入的名字只是:Harry Smith
java.lang.StringIndexOutOfBoundsException: String index out of range: -7
和
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
当名字叫莎拉时
这是我的代码,但我不确定如何修复它:
public class Names {
public static String lastNameFirst(String name) {
int firstIndex = name.indexOf(" ");
int secondIndex = name.indexOf(" ", firstIndex + 1);
String firstName = name.substring(0, name.indexOf(" "));
String middleName = name.substring(firstIndex + 1, secondIndex);
String lastName = name.substring(secondIndex + 1);
String result = "";
result = lastName + ", " + firstName + " " + middleName;
return result;
}
}
提前致谢!!
您的代码假定输入字符串至少包含两个空格。当该假设错误时(如输入 "Harry Smith" 和 "Sarah"),您会得到一个异常。
在使用它们的值之前,您必须检查 firstIndex
和 secondIndex
是否为正数。
使用 split 和 switch 会容易得多
String name = "Mary Jane Lee";
String arr[] = name.split (" ");
switch (arr.length) {
case 1:
System.out.println(name);
break;
case 2:
System.out.println(arr[1] + ", " + arr[0]);
break;
default:
System.out.println(arr[2] + ", " + arr[0] + " " + arr[1]);
}
更稳健的方法是使用lastIndexOf
找到最后一个space:
int lastSpace = name.lastIndexOf(' ');
if (lastSpace != -1) {
String lastName = name.substring(lastSpace + 1);
String partBeforeLastName = name.substring(0, lastSpace);
return lastName + ", " + partBeforeLastName;
} else {
return name;
}
您实际上并不真正关心另一个 space(如果它存在的话),因为名字和中间名保持相同的相对顺序。
(一般来说,有lots of falsehoods that programmers believe about names;但为了练习的目的,让我们把它们放在一边。)
问题是你的代码除了有 3 个名字。名字少时不处理
public static String lastNameFirst(String name)
{
int firstIndex = name.indexOf(" ");
if ( firstIndex >= 0 )
{
int secondIndex = name.indexOf(" ", firstIndex + 1 );
String firstName = name.substring(0, firstIndex);
if ( secondIndex >= 0 ) // we have 3 names
{
String middleName = name.substring(firstIndex + 1, secondIndex);
String lastName = name.substring(secondIndex + 1);
return lastName + ", " + firstName + " " + middleName;
}
else // we have 2 names
{
String lastName = name.substring(firstIndex + 1);
return lastName + ", " + firstName;
}
}
else // have only one name
return name;
}
也值得一试lastIndexOf()
:
public static String lastNameFirst(String name)
{
int lastIndex = name.lastIndexOf(" ");
if ( lastIndex >= 0 ) // have at least 2 names
{
String firstNames = name.substring(0,lastIndex);
String lastName = name.substring(lastIndex + 1);
return lastName + ", " + firstNames;
}
}
else // have only one name
return name;
}
此外,可以尝试不同的方法,将名称拆分为一个数组,如下所示:
public static String lastNameFirst(String name)
{
String[] parts = name.split(" ");
switch ( parts.length )
{
case 1:
return name;
case 2:
return parts[1] + ", " + parts[0];
case 3:
return parts[2] + ", " + parts[0] + " " + parts[1];
}
}