简体   繁体   中英

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

Calling Method 'OrderByMaths' in Object 'ExtracDig' of Class 'digitExtractor', with User Input String....'HACK'

OrderbyMatch : Inside Method 'OrderbyMaths'....

OrderbyMatch : Initializing a variable of type int 'tNumberLength'.

OrderbyMatch : Variable Initialized of type int 'tNumberLength'.

OrderbyMaths : Capture length of User Input String into Variable 'tNumberLength'.

OrderbyMaths : The length of User Input String in Variable 'tNumberLength' is '4'.

OrderbyMatch : Initializing an Array of type int 'arrNumberSequence'; with Size of Array equal to 'tNumberLength'.

OrderbyMatch : Initialized Array 'arrNumberSequence' with Size '4'

OrderbyMatch : Value in Array Slot 'H' is 'H'

OrderbyMatch : Value in Array Slot 'A' is 'A'

OrderbyMatch : Value in Array Slot 'C' is 'C'

OrderbyMatch : Value in Array Slot 'K' is '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)

Regards

Dwenish

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

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) 

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

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. Your loop iterates from 0 to 4 (including 4), so in the last iteration it calls arrNumberSequence[4] which is out of index.

Just remove the '=' and you'll be fine:

while (i < arrNumberSequence.length)

Change this:

 while (i <= arrNumberSequence.length)

into this:

 while (i < arrNumberSequence.length)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM