Java 尝试用空格拆分字符串
我正在写一段代码,我试图使用用户输入的值之间的空格,将用户的输入分成3个不同的数组。但是,每次运行代码时都会出现错误:Java 尝试用空格拆分字符串,java,arrays,split,delimiter,Java,Arrays,Split,Delimiter,我正在写一段代码,我试图使用用户输入的值之间的空格,将用户的输入分成3个不同的数组。但是,每次运行代码时都会出现错误: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at Substring.main(Substring.java:18) Java Result: 1 我曾尝试在输入文本时使用不同的分隔符,但效果很好,例如,正常使用a/拆分完全相同的输入,并完
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Substring.main(Substring.java:18)
Java Result: 1
我曾尝试在输入文本时使用不同的分隔符,但效果很好,例如,正常使用a/拆分完全相同的输入,并完成了我希望它完成的操作。
任何帮助都将不胜感激!
如果需要,这是我的代码
import java.util.Scanner;
public class Substring{
public static void main(String[]args){
Scanner user_input = new Scanner(System.in);
String fullname = ""; //declaring a variable so the user can enter their full name
String[] NameSplit = new String[2];
String FirstName;
String MiddleName;
String LastName;
System.out.println("Enter your full name (First Middle Last): ");
fullname = user_input.next(); //saving the user's name in the string fullname
NameSplit = fullname.split(" ");//We are splitting up the value of fullname every time there is a space between words
FirstName = NameSplit[0]; //Putting the values that are in the array into seperate string values, so they are easier to handle
MiddleName = NameSplit[1];
LastName = NameSplit[2];
System.out.println(fullname); //outputting the user's orginal input
System.out.println(LastName+ ", "+ FirstName +" "+ MiddleName);//outputting the last name first, then the first name, then the middle name
new StringBuilder(FirstName).reverse().toString();
System.out.println(FirstName);
}
}Split是一个正则表达式,您可以查找一个或多个空格(“+”),而不仅仅是一个空格(“”) 也可以使用Strint标记器
String message = "MY name is ";
String delim = " \n\r\t,.;"; //insert here all delimitators
StringTokenizer st = new StringTokenizer(message,delim);
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
您在以下地方犯了错误: fullname=user_input.next() 它应该是
nextLine()
String[]NameSplit=新字符串[2]
当您稍后执行NameSplit=user\u input.split(…)
时,不需要执行此步骤,但它应该是new String[3]
而不是new String[2]
,因为您存储的是三个条目,即名字、中间名和姓氏
以下是正确的程序:
class Substring {
public static void main (String[] args) throws java.lang.Exception {
Scanner user_input = new Scanner(System.in);
String[] NameSplit = new String[3];
String FirstName;
String MiddleName;
String LastName;
System.out.println("Enter your full name (First Middle Last): ");
String fullname = user_input.nextLine();
NameSplit = fullname.split(" ");
FirstName = NameSplit[0];
MiddleName = NameSplit[1];
LastName = NameSplit[2];
System.out.println(fullname);
System.out.println(LastName+ ", "+ FirstName +" "+ MiddleName);
new StringBuilder(FirstName).reverse().toString();
System.out.println(FirstName);
}
}
输出:
输入您的全名(前中后):John Mayer Smith
史密斯,约翰·梅尔
约翰
java.util.Scanner
使用定界符模式将其输入分解为标记,默认情况下,定界符模式匹配空白。
因此,即使您输入了“Elvis John Presley”,只有“Elvis”存储在fullName
变量中。
您可以使用BufferedReader读取全文:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
fullname = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
或者,您可以使用以下命令更改扫描仪的默认行为:
user\u input.useDelimiter(“\n”)代码>方法。异常清楚地表明您超出了数组的长度。LastName=NameSplit[2]
中的索引2
超出了数组的界限。要消除此错误,您必须:
1-将String[]NameSplit=new String[2]
更改为String[]NameSplit=new String[3]
,因为数组长度应为3
请在此处阅读更多内容:[]
到目前为止,错误已经消失,但解决方案还不正确,因为NameSplit[1]
和NameSplit[2]
都是null
,因为user\u input.next()
只读取第一个单词(*基本上直到检测到空白(如果只有一个单词,则为“\n”)。因此:
2-更改用户输入。下一步()
touser_input.nextLine()因为nextLine()
读取整行(*基本上直到检测到“\n”为止)
请在此处阅读更多信息:[为什么要使用fullname.split(\\”)而不是fullname.split(“”)?我很确定我以前做过,而且很有效。是的,但你没有告诉我们你打了什么,所以..那不应该在那里,我的孩子输入了要求的内容,例如“埃尔维斯·约翰·普雷斯利”尝试打印全名。我看不出有什么问题。这是字符串[2],因为第一个值是0,第二个值是1,第三个值是2:),但还是要感谢您的帮助@L.Jones 28请记住,索引从0开始,当您保留空格时,索引的概念不再适用。所以它应该是3
,而不仅仅是2
。
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
fullname = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}