英文:
String index out of range on space bar character
问题
例如,名称Donald Trump(12个字符)会引发错误字符串索引超出范围7(其中找到空格),即使名称Donald Trump更长。
package test;
import javax.swing.JOptionPane;
public class Usernamesubstring {
public static void main(String[] args) {
String fullname = JOptionPane.showInputDialog("What is your full name");
int breakbetween = fullname.lastIndexOf(" ");
String firstnamess = fullname.substring(breakbetween - 3, breakbetween);
int length = fullname.length();
String lastnamess = fullname.substring(length - 3, length);
String firstnamec = firstnamess.substring(0, 0);
String lastnamec = lastnamess.substring(breakbetween + 1, breakbetween + 1);
firstnamec = firstnamec.toUpperCase();
lastnamec = lastnamec.toUpperCase();
String firstname = firstnamess.substring(1, 3);
String lastname = firstnamess.substring(1, 3);
firstname = firstnamec + firstname;
lastname = lastnamec + lastname;
System.out.println(firstname + lastname);
}
}
线程中的异常"main" java.lang.StringIndexOutOfBoundsException: 字符串索引超出范围: 7
在java.lang.String.substring(String.java:1963)
在test.Usernamesubstring.main(Usernamesubstring.java:14)
英文:
For example the name Donald trump (12 character) brings up the error string index out of range 7 (where the space is found) even though the name Donald trump is longer.
package test;
import javax.swing.JOptionPane;
public class Usernamesubstring {
public static void main(String[] args) {
String fullname = JOptionPane.showInputDialog("What is your full name");
int breakbetween = fullname.lastIndexOf(" ");
String firstnamess = fullname.substring(breakbetween - 3, breakbetween);
int length = fullname.length();
String lastnamess = fullname.substring(length - 3, length);
String firstnamec = firstnamess.substring(0, 0);
String lastnamec = lastnamess.substring(breakbetween + 1, breakbetween + 1 );
firstnamec = firstnamec.toUpperCase();
lastnamec = lastnamec.toUpperCase();
String firstname = firstnamess.substring(1,3);
String lastname = firstnamess.substring(1,3);
firstname = firstnamec + firstname;
lastname = lastnamec + lastname;
System.out.println(firstname + lastname);
}
}
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 7
at java.lang.String.substring(String.java:1963)
at test.Usernamesubstring.main(Usernamesubstring.java:14)
答案1
得分: 1
你把它弄得比它需要的更复杂了。可以使用String.split
制作一个简单的解决方案(它根据分隔符将字符串分割为较小的字符串数组,例如"Donald Trump".split(" ") == {"Donald", "Trump"}
) <br>
完整的代码
class Usernamesubstring // 由于不再使用子字符串,所以要更改
{
public static void main(String[] args)
{
String fullName = "Donald Trump";
String[] parts = fullName.split(" ");
String firstName = parts[0]; // 空格之前的第一个项目
String lastName = parts[parts.length - 1]; // 数组中的最后一个项目
System.out.println(firstName + " " + lastName);
}
}
英文:
You've made it more complicated than it needs to be. A simple solution can be made using String.split
(which divides a string into an array of smaller strings based on a delimiter, e.g. "Donald Trump".split(" ") == {"Donald", "Trump"}
) <br>
Full Code
class Usernamesubstring // change that since it no longer uses substrings
{
public static void main (String[] args)
{
String fullName = "Donald Trump";
String[] parts = fullName.split(" ");
String firstName = parts[0]; // first item before the space
String lastName = parts[parts.length - 1]; // last item in the array
System.out.println(firstName + " " + lastName);
}
}
答案2
得分: 1
有时独立于您的索引
String fullName = "Donald Trump";
String[] result = fullName.split(" ");
在结果中,您现在会找到
result [0] ==> Donald
result 1 ==> Trump
这对您的项目不是更容易吗?
英文:
sometimes independent of your indexes
String fullName = "Donald Trump";
String[] result = fullName.split (" ");
in result you will find now
result [0] ==> Donald
result 1 ==> Trump
isn't that a little easier for your project?
答案3
得分: 1
你的错误应该在这一行:String lastnamec = lastnamess.substring(breakbetween + 1, breakbetween + 1);
,因为lastnamess
是从 fullname.substring(length - 3, length);
得到的一个长度为3的字符串,而 breakbetween
大于3,对于 "Donald Trump" 这个字符串来说,空格是第6个字符。
你应该简化一下你的代码,这样更容易阅读并找到问题。
英文:
Your error shoul be in the line String lastnamec = lastnamess.substring(breakbetween + 1, breakbetween + 1 );
as lastnamess is a string of lenght 3 from fullname.substring(length - 3, length);
and breakbetween is greater then 3 for "Donald Trump", where space is character 6.
You should simpify your code a bit, it makes it easier to read and find the problems.
答案4
得分: 0
tl;dr: 当您尝试访问字符串的索引超出其长度或不包含在字符串中(负值)时,异常会发生。
关于您的方法:通常不建议要求输入完整姓名,因为人们往往输入奇怪的东西或混淆顺序。最好分别要求输入名字和姓氏。
假设有人以 Firstname Lastname
输入他的名字,您不必进行如此复杂的子字符串操作,Java 有一些不错的功能:
String name = "Mario Peach Bowser";
name = name.trim();
String[] parts = name.split(" ");
String lastname = parts[parts.length-1];
String firstname = name.replace(lastname, "").trim();
System.out.println("Hello "+firstname+", your last name is: "+lastname);
在这种情况下,我使用 trim()
函数来删除开头和结尾的空格,并在出现空格时拆分字符串。由于人们可能有一些中间名字之类的东西,我只是从原始输入字符串中替换了姓氏,再次调用 trim()
,然后您就可以提取出所有内容。
如果您真的想要子字符串方法,以下方法也可以:
String lastname = name.substring(name.lastIndexOf(" ")).trim();
String firstname = name.substring(0,name.lastIndexOf(" ")).trim();
通常情况下,不需要存储索引变量。但每个变体都需要某种形式的错误检查,您可以使用 try{}
和 catch()
,或在解析之前检查 String
。
英文:
tl;dr: The exception occurs when you try to access a String at an index which exceeds it's length or is just not contained in the string (negative values).
Regarding your approach: It's usually not a good idea to prompt a name in full because people tend to input weird stuff or mix up the order. Better prompt for first and last name separately.
Assuming someone input his name with Firstname Lastname
you wouldn't have to make such a substring mess, Java has some nice features:
String name = "Mario Peach Bowser";
name = name.trim();
String[] parts = name.split(" ");
String lastname = parts[parts.length-1];
String firstname = name.replace(lastname, "").trim();
System.out.println("Hello "+firstname+", your last name is: "+lastname);
In this case I am using the trim()
function to remove whitespaces at the start and end and just split the string when a white space occurs. Since people can have some middle names and stuff, I just replace the last name out of the raw input string, call trim()
on it again and you have everything extracted.
If you really want a substring approach, the following would work:
String lastname = name.substring(name.lastIndexOf(" ")).trim();
String firstname = name.substring(0,name.lastIndexOf(" ")).trim();
You usually don't store the index variables. But each variant would need some sort of error check, you can either use try{}
and catch()
or check the String
before parsing.
答案5
得分: 0
只需要这些行。
String[] nameArr = fullname.split(" ");
String lastN = nameArr[nameArr.length - 1];
int lastIndexOf = fullname.lastIndexOf(lastN);
String firstN = fullname.substring(0, lastIndexOf);
System.out.println(firstN + " " + lastN);
英文:
Only these lines are required.
String[] nameArr = fullname.split(" ");
String lastN = nameArr[nameArr.length - 1];
int lastIndexOf = fullname.lastIndexOf(lastN);
String firstN = fullname.substring(0, lastIndexOf);
System.out.println(firstN + " " + lastN);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论