[英]Java : Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range:
Am a beginner, could anyone help me figure out what us going on. 是一个初学者,谁能帮助我弄清楚我们的情况。 Am Trying to read a String and store each character of the String in an Array.
我正在尝试读取字符串,并将字符串的每个字符存储在数组中。
import java.util.Scanner;
public class CoreMainDigitExtractor {
static Scanner inputString = new Scanner(System.in);
public static void main(String[] args) {
digitExtractor ExtracDig = new digitExtractor();
System.out.println("Enter a String to Extract and Display Vertically in Reverse Order : ");
String input1 = inputString.nextLine();
System.out.println("User input String is : " + input1);
System.out.println("Calling Method 'OrderByMaths' in Object 'ExtracDig' of Class 'digitExtractor', with User Input String....'"+input1+"'");
ExtracDig.OrderbyMaths(input1);
}
}
public class digitExtractor {
int tNumber;
public digitExtractor() {
// TODO Auto-generated constructor stub
}
public void OrderbyMaths(String numberSequence) {
System.out.println("OrderbyMatch : Inside Method 'OrderbyMaths'....");
System.out.println("OrderbyMatch : Initializing a variable of type int 'tNumberLength'.");
int tNumberLength = numberSequence.length();
System.out.println("OrderbyMatch : Variable Initialized of type int 'tNumberLength'.");
System.out.println("OrderbyMaths : Capture length of User Input String into Variable 'tNumberLength'.");
System.out.println("OrderbyMaths : The length of User Input String in Variable 'tNumberLength' is '" + tNumberLength + "'.");
System.out.println("OrderbyMatch : Initializing an Array of type int 'arrNumberSequence'; with Size of Array equal to 'tNumberLength'.");
char arrNumberSequence[] = new char[tNumberLength];
System.out.println("OrderbyMatch : Initialized Array 'arrNumberSequence' with Size '" + arrNumberSequence.length + "'");
int i = 0;
while (i <= arrNumberSequence.length){
arrNumberSequence[i] = numberSequence.charAt(i);
System.out.println("OrderbyMatch : Value in Array Slot '"+arrNumberSequence[i]+"' is '"+ numberSequence.charAt(i)+"'");
i++;
}
}
}
OUTPUT 输出值
Enter a String to Extract and Display Vertically in Reverse Order : 输入一个字符串以反向提取并垂直显示:
HACK 哈克
User input String is : HACK 用户输入的字符串是:HACK
Calling Method 'OrderByMaths' in Object 'ExtracDig' of Class 'digitExtractor', with User Input String....'HACK' 使用用户输入字符串....“ HACK”调用类“ digitExtractor”的对象“ ExtracDig”中的方法“ OrderByMaths”
OrderbyMatch : Inside Method 'OrderbyMaths'.... OrderbyMatch:内部方法'OrderbyMaths'....
OrderbyMatch : Initializing a variable of type int 'tNumberLength'. OrderbyMatch:初始化一个类型为int'tNumberLength'的变量。
OrderbyMatch : Variable Initialized of type int 'tNumberLength'. OrderbyMatch:变量,类型为int'tNumberLength'。
OrderbyMaths : Capture length of User Input String into Variable 'tNumberLength'. OrderbyMaths:将用户输入字符串的长度捕获到变量'tNumberLength'中。
OrderbyMaths : The length of User Input String in Variable 'tNumberLength' is '4'. OrderbyMaths:变量“ tNumberLength”中用户输入字符串的长度为“ 4”。
OrderbyMatch : Initializing an Array of type int 'arrNumberSequence'; OrderbyMatch:初始化一个类型为'arrNumberSequence'的数组; with Size of Array equal to 'tNumberLength'.
Array的大小等于'tNumberLength'。
OrderbyMatch : Initialized Array 'arrNumberSequence' with Size '4' OrderbyMatch:大小为“ 4”的初始化数组“ arrNumberSequence”
OrderbyMatch : Value in Array Slot 'H' is 'H' OrderbyMatch:阵列插槽“ H”中的值为“ H”
OrderbyMatch : Value in Array Slot 'A' is 'A' OrderbyMatch:阵列插槽“ A”中的值为“ A”
OrderbyMatch : Value in Array Slot 'C' is 'C' OrderbyMatch:阵列插槽“ C”中的值为“ C”
OrderbyMatch : Value in Array Slot 'K' is 'K' OrderbyMatch:阵列插槽“ K”中的值为“ K”
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4 at java.lang.String.charAt(Unknown Source) at digitExtractor.OrderbyMaths(digitExtractor.java:23) at CoreMainDigitExtractor.main(CoreMainDigitExtractor.java:16) 线程“主”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:java.lang.String.charAt(未知源)为4,digitExtractor.OrderbyMaths(digitExtractor.java:23)在CoreMainDigitExtractor.main(CoreMainDigitExtractor.java) :16)
Regards 问候
Dwenish Dwenish
For the string HACK
we have a .length
of 4
. 对于字符串
HACK
我们的.length
为4
。 Indexes in Java start at 0 so we have Java中的索引从0开始,所以我们有
['H','A','C','K']
^ ^ ^ ^
0 1 2 3
So lets say in your while loop i=4
因此,可以说在您的while循环中
i=4
while (i <= arrNumberSequence.length)
while (is 4 less than or equal to 4? yes 4==4 so continue with the loop)
When we try to access charAt(4)
it is not a valid index for our string 当我们尝试访问
charAt(4)
它不是字符串的有效索引
['H','A','C','K']
^ ^ ^ ^ ^
0 1 2 3 4
So we get the exception 所以我们得到了例外
java.lang.StringIndexOutOfBoundsException: String index out of range: 4
The problem is in here: 问题在这里:
while (i <= arrNumberSequence.length){
The length of the string "HACK" is 4. The indexing is 0, 1, 2 ,3. 字符串“ HACK”的长度为4。索引为0、1、2、3。 Your loop iterates from 0 to 4 (including 4), so in the last iteration it calls arrNumberSequence[4] which is out of index.
您的循环从0迭代到4(包括4),因此在上一次迭代中,它调用arrNumberSequence [4],该索引不在索引内。
Just remove the '=' and you'll be fine: 只需删除'=',就可以了:
while (i < arrNumberSequence.length)
Change this: 更改此:
while (i <= arrNumberSequence.length)
into this: 到这个:
while (i < arrNumberSequence.length)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.