Java fName.substring超出范围 公共类注册{ 公共静态void main(字符串[]args){ 最后一个字符串MY_DELIMITER=“”,“; 布尔值tryAgain=true; 字符串fName=“”; 字符串A=fName.substring(0,2); 字符串lName=“”; int lName length=lName.length(); 字符串B=lName.substring(lNameLength-4,lNameLength); 字符串地址=”; 字符串zip=“”; 字符串C=zip.substring(0,5); 字符串年龄=”; 字符串D=年龄。子字符串(0,1); 字符串性别=”; 弦乐比赛=”; 字符串regList=“”; Scanner myScanner=新扫描仪(System.in); 布尔showList=false; //从用户处获取输入,直到他们键入“q” //对于每个输入检查“q” //如果不是q,则追加输入 //添加到现有字符串+分隔符 while(tryAgain) { System.out.println(“名称:(q退出)”; fName=myScanner.nextLine(); System.out.println(“姓氏:(q退出)”; lName=myScanner.nextLine(); System.out.println(“Addess:”); address=myScanner.nextLine(); System.out.println(“年龄:”); age=myScanner.nextLine(); System.out.println(“性别:”); 性别=myScanner.nextLine(); System.out.println(“种族:”); race=myScanner.nextLine(); 如果(fName.equals(“q”)) { tryAgain=false; } 其他的 { //使用分隔符将新名称附加到列表中 regList=fName+lName+“\n”+地址+“\n”+年龄+“\n”+性别+“\n”+种族+我的分隔符; } }//while结束() System.out.println(“这是您的注册:“+regList”); //使用相同的分隔符将字符串转换为数组 字符串[]regArray=regList.split(我的分隔符); //询问用户是否要显示数组的内容 //如果为“y”,则使用foreach循环显示列表 System.out.println(“您想从数组中查看注册吗?[y-n]”); fName=myScanner.nextLine(); myScanner.close(); fName=fName.toLowerCase(); showList=fName.equals(“y”)?真:假; 如果(显示列表) { //显示每个应用程序的结果 System.out.println(“这是您在数组中的注册:”); //使用for-each语句,而不是更复杂的for()循环 //对于(int counter=0;计数器
我试图提取fName输入的前3个字母,所以我想我可以使用fName.substring来做这件事,但它给了我这个错误Java fName.substring超出范围 公共类注册{ 公共静态void main(字符串[]args){ 最后一个字符串MY_DELIMITER=“”,“; 布尔值tryAgain=true; 字符串fName=“”; 字符串A=fName.substring(0,2); 字符串lName=“”; int lName length=lName.length(); 字符串B=lName.substring(lNameLength-4,lNameLength); 字符串地址=”; 字符串zip=“”; 字符串C=zip.substring(0,5); 字符串年龄=”; 字符串D=年龄。子字符串(0,1); 字符串性别=”; 弦乐比赛=”; 字符串regList=“”; Scanner myScanner=新扫描仪(System.in); 布尔showList=false; //从用户处获取输入,直到他们键入“q” //对于每个输入检查“q” //如果不是q,则追加输入 //添加到现有字符串+分隔符 while(tryAgain) { System.out.println(“名称:(q退出)”; fName=myScanner.nextLine(); System.out.println(“姓氏:(q退出)”; lName=myScanner.nextLine(); System.out.println(“Addess:”); address=myScanner.nextLine(); System.out.println(“年龄:”); age=myScanner.nextLine(); System.out.println(“性别:”); 性别=myScanner.nextLine(); System.out.println(“种族:”); race=myScanner.nextLine(); 如果(fName.equals(“q”)) { tryAgain=false; } 其他的 { //使用分隔符将新名称附加到列表中 regList=fName+lName+“\n”+地址+“\n”+年龄+“\n”+性别+“\n”+种族+我的分隔符; } }//while结束() System.out.println(“这是您的注册:“+regList”); //使用相同的分隔符将字符串转换为数组 字符串[]regArray=regList.split(我的分隔符); //询问用户是否要显示数组的内容 //如果为“y”,则使用foreach循环显示列表 System.out.println(“您想从数组中查看注册吗?[y-n]”); fName=myScanner.nextLine(); myScanner.close(); fName=fName.toLowerCase(); showList=fName.equals(“y”)?真:假; 如果(显示列表) { //显示每个应用程序的结果 System.out.println(“这是您在数组中的注册:”); //使用for-each语句,而不是更复杂的for()循环 //对于(int counter=0;计数器,java,eclipse,Java,Eclipse,我试图提取fName输入的前3个字母,所以我想我可以使用fName.substring来做这件事,但它给了我这个错误 很抱歉,为了节省时间,我没有添加所有代码。显然,这看起来令人困惑。因此fName输入是用户的名称。不能按那个顺序吗 在为任何内容添加子字符串之前,请始终检查字符串的长度。尤其是当用户给你这个变量时。呃……你的操作顺序是可疑的。实际上,到处都是 请看以下交互: public class Registration { public static void main(Stri
很抱歉,为了节省时间,我没有添加所有代码。显然,这看起来令人困惑。因此fName输入是用户的名称。不能按那个顺序吗 在为任何内容添加子字符串之前,请始终检查字符串的长度。尤其是当用户给你这个变量时。呃……你的操作顺序是可疑的。实际上,到处都是 请看以下交互:
public class Registration {
public static void main(String[] args) {
final String MY_DELIMITER = "','";
boolean tryAgain = true;
String fName = "";
String A = fName.substring(0,2);
String lName = "";
int lNameLength = lName.length();
String B = lName.substring(lNameLength-4,lNameLength);
String address = "";
String zip = "";
String C = zip.substring(0,5);
String age = "";
String D = age.substring(0,1);
String gender = "";
String race = "";
String regList = "";
Scanner myScanner = new Scanner(System.in);
boolean showList = false;
// Get input from the user until they type "q"
// For each input check for "q"
// if not q, append the input
// to the existing String + the delimiter
while(tryAgain)
{
System.out.println("Name: (q to quit)");
fName = myScanner.nextLine();
System.out.println("Last Name: (q to quit)");
lName = myScanner.nextLine();
System.out.println("Addess: ");
address = myScanner.nextLine();
System.out.println("Age: ");
age = myScanner.nextLine();
System.out.println("Gender: ");
gender = myScanner.nextLine();
System.out.println("Race: ");
race = myScanner.nextLine();
if(fName.equals("q"))
{
tryAgain = false;
}
else
{
// Append new name to the list using a delimiter
regList = fName + lName + "\n" + address + "\n" + age + "\n" + gender + "\n" + race + MY_DELIMITER;
}
} // end of while( )
System.out.println("Here is your registration:" + regList);
// Convert the String into an array, using the same delimiter
String[ ] regArray = regList.split(MY_DELIMITER);
// Ask the user if they want to display the contents of the array
// If "y" then display the list using a foreach loop
System.out.println("Would you like to see the registration from the Array? [y-n]");
fName = myScanner.nextLine( );
myScanner.close();
fName = fName.toLowerCase( );
showList = fName.equals("y")?true:false;
if(showList)
{
// Display the results using for each
System.out.println("Here is your registration from the array: ");
// Use a for each statement instead of the more complex for( ) loop
// for(int counter=0; counter < employeeArray.length; counter++)
for(String thisReg:regArray)
{
System.out.println(thisReg);
System.out.printf("USER ID: ", A + "-" + B + "-" + C + "-" + D);
}
} // end of if(showList)
}
}
声明一个空字符串,然后立即获取它的子字符串。您从哪里获取子字符串的数据?这里没有子字符串-空字符串的长度为零
在获取字符串的子字符串之前,您应该确定您正在将数据放入字符串中。使用扫描仪
在这里会有很大的帮助
或者更好的方法是,将您的
myScanner
实例移动到main
的顶部,这样可以更清楚地知道它应该放在哪里,以及应该如何工作。您正在尝试获取空字符串的子字符串
String fName = "";
String A = fName.substring(0,2);
将fName更改为一些实际的字符串
,并在调用子字符串之前检查字符串的长度
,以确保所需大小的子字符串存在
String fName = "";
String A = fName.substring(0,2); // here fName is empty!!!
所有其他的
字符串也都是这样。你认为空字符串的前三个字母应该是什么?。名称的输入是什么?也许我没有看到,但程序是如何开始获取值的?我对java(obv)不熟悉,我习惯于在代码开始时声明变量。
String fName = "somestring";
if(fName.length() >= 2) {
String A = fName.substring(0,2);
System.out.println(A); // prints out "so"
}