Java:线程“main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围: [英] Java : Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range:
问题描述
初学者,任何人都可以帮我弄清楚我们在做什么。我试图读取一个String并将String的每个字符存储在一个数组中。
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++;
}
}
}
输出
输入一个字符串以反向顺序提取并垂直显示:
Enter a String to Extract and Display Vertically in Reverse Order :
HACK
用户输入字符串是:HACK
User input String is : HACK
在类'digitExtractor的对象'ExtracDig'中调用方法'OrderByMaths' ',用户输入字符串....'HACK'
Calling Method 'OrderByMaths' in Object 'ExtracDig' of Class 'digitExtractor', with User Input String....'HACK'
OrderbyMatch:内部方法'OrderbyMaths'....
OrderbyMatch : Inside Method 'OrderbyMaths'....
OrderbyMatch:初始化int'tNumberLength'类型的变量。
OrderbyMatch : Initializing a variable of type int 'tNumberLength'.
OrderbyMatch:变量初始化类型为int'tNumberLength'。
OrderbyMatch : Variable Initialized of type int 'tNumberLength'.
OrderbyMaths:将用户输入字符串的长度捕获到变量'tNumberLength'。
OrderbyMaths : Capture length of User Input String into Variable 'tNumberLength'.
OrderbyMaths:变量'tNumberLength'中用户输入字符串的长度为'4'。
OrderbyMaths : The length of User Input String in Variable 'tNumberLength' is '4'.
OrderbyMatch:初始化int'arrNumberSequence'类型的数组;数组大小等于'tNumberLength'。
OrderbyMatch : Initializing an Array of type int 'arrNumberSequence'; with Size of Array equal to 'tNumberLength'.
OrderbyMatch:初始化数组'arrNumberSequence',大小'4'
OrderbyMatch : Initialized Array 'arrNumberSequence' with Size '4'
OrderbyMatch:数组插槽中的值'H'是'H'
OrderbyMatch : Value in Array Slot 'H' is 'H'
OrderbyMatch:数组插槽中的值'A'是'A'
OrderbyMatch : Value in Array Slot 'A' is 'A'
OrderbyMatch:数组插槽中的值'C'是'C'
OrderbyMatch : Value in Array Slot 'C' is 'C'
OrderbyMatch:数组插槽中的值'K'是'K'
OrderbyMatch : Value in Array Slot 'K' is 'K'
线程main中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:4
at java.lang.String.charAt(Unknown Source)
at digitExtractor.OrderbyMaths(digitExtractor.java:23)
at CoreMainDigitExtractor.main(CoreMainDigitExtractor.java:16)
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)
问候
Dwenish
推荐答案
对于字符串 HACK
我们有 .length
4
。 Java中的索引从0开始,所以我们有
For the string HACK
we have a .length
of 4
. Indexes in Java start at 0 so we have
['H','A','C','K']
^ ^ ^ ^
0 1 2 3
所以,请在你的while循环 i = 4
So lets say in your while loop i=4
while (i <= arrNumberSequence.length)
while (is 4 less than or equal to 4? yes 4==4 so continue with the loop)
当我们尝试访问 charAt(4)
时,它不是我们字符串的有效索引
When we try to access charAt(4)
it is not a valid index for our string
['H','A','C','K']
^ ^ ^ ^ ^
0 1 2 3 4
因此我们得到例外
java.lang.StringIndexOutOfBoundsException: String index out of range: 4
这篇关于Java:线程“main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!