小编典典

Java:线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:

java

是一个初学者,谁能帮助我弄清楚我们的情况。我正在尝试读取字符串,并将字符串的每个字符存储在数组中。

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++;

        }

    }

}

输出值

输入一个字符串以相反的顺序垂直提取和显示:

哈克

用户输入的字符串是:HACK

使用用户输入字符串....’HACK’调用类’digitExtractor’的对象’ExtracDig’中的方法’OrderByMaths’

OrderbyMatch:内部方法“ OrderbyMaths”…。

OrderbyMatch:初始化一个类型为int’tNumberLength’的变量。

OrderbyMatch:变量,类型为int’tNumberLength’。

OrderbyMaths:将用户输入字符串的长度捕获到变量’tNumberLength’中。

OrderbyMaths:变量“ tNumberLength”中用户输入字符串的长度为“ 4”。

OrderbyMatch:初始化一个类型为’arrNumberSequence’的数组;Array的大小等于’tNumberLength’。

OrderbyMatch:大小为“ 4”的初始化数组“ arrNumberSequence”

OrderbyMatch:阵列插槽“ H”中的值为“ H”

OrderbyMatch:阵列插槽“ A”中的值为“ A”

OrderbyMatch:阵列插槽“ C”中的值为“ C”

OrderbyMatch:阵列插槽“ K”中的值为“ K”

线程“主”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:java.lang.String.charAt(未知源)为4,digitExtractor.OrderbyMaths(digitExtractor.java:23)位于CoreMainDigitExtractor.main(CoreMainDigitExtractor.java)
:16)

问候

Dwenish


阅读 305

收藏
2020-12-03

共1个答案

小编典典

对于字符串HACK,我们有一个.length4。Java中的索引从0开始,所以我们有

['H','A','C','K']
  ^   ^   ^   ^ 
  0   1   2   3

所以让我们在while循环中说 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)它不是我们字符串的有效索引

['H','A','C','K']
  ^   ^   ^   ^   ^
  0   1   2   3   4

所以我们得到了例外

java.lang.StringIndexOutOfBoundsException: String index out of range: 4
2020-12-03