AeriaGlorisia
AeriaGlorisia

Reputation: 229

String index out of range - Why is this occurring?

(Please keep in mind I have only been studying java for under a month on my own)

I am trying to make a program that simply tells you the last char of the name you give the program. Here is my code:

import java.util.Scanner;


public class LastCharacter {


public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    System.out.println("hey");
    String name = reader.nextLine();
    lastChar(name);
}

public static char lastChar(String text) {
    char lastChar = '\0';;
    int i = 0;
    for (i = 0; i <= text.length(); i++) {
        lastChar = text.charAt(i);

    }
    System.out.println(lastChar);
    return lastChar;
}

}

Error: 
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.charAt(String.java:658)
at LastCharacter.lastChar(LastCharacter.java:19)
at LastCharacter.main(LastCharacter.java:11)

Java Result: 1

I also know this can be made by subtracting the length of the string by 1, however I would like to know why this method isn't working. I don't really know how to word this but do strings and chars not get along? (pls dont make fun of me)

Thanks!

Upvotes: 1

Views: 2230

Answers (5)

Frank Ker
Frank Ker

Reputation: 21

Two things to take note here:

1.) The length() method in Java String class returns the number of characters of a string

2.) Java arrays uses zero-base index

So, to accomplish your task of getting the last character of the name string :

public static char lastChar(String text) {
    int textLength = text.length();
    char lastChar = text.charAt(textLength - 1);  //first char starts from index 0
    return lastChar;
}

Hope it helps.

Upvotes: 1

Justin Conroy
Justin Conroy

Reputation: 376

Java strings start at a base index of 0. Therefore, this line: for (i = 0; i <= text.length(); i++) { is trying to access an index that doesn't exist. The string main only goes from 0 to 3. So, when you try to access index 4, you get the out of bounds error.

Replace this line: for (i = 0; i <= text.length(); i++) {

With this: for (i = 0; i < text.length(); i++) { to fix the problem.

Upvotes: 6

nhouser9
nhouser9

Reputation: 6780

Strings are 0 based, meaning the first index is 0. So for the string "mom", the 0th index is "m", the 1st index is "o" and the 2nd index is "m". That means this string doesn't have a third index, even though its length is 3! Based on that, your loop should be:

for (i = 0; i < text.length(); i++) {
    lastChar = text.charAt(i);
}

However, there is an even better way to do it with no loops at all. We can simply get the character at the last index of the string without looping over each character. It is less complicated and more efficent:

lastChar = text.charAt(text.length() - 1);

Upvotes: 0

Tah
Tah

Reputation: 1536

The problem is because Java uses a 0 index array for the string. This means that your for loop i <= text.length() is going to the last character +1. In a name like "Joe"

J = 0, o = 1, e = 2

The length of "Joe" is 3 and therefor the loop goes to index(3) which is out of the bounds of the character array.

Upvotes: 2

Diego
Diego

Reputation: 314

You are out of bounds! The condition should be: i < text.length()

for (i = 0; i < text.length(); i++) {
    lastChar = text.charAt(i);

}

Upvotes: 0

Related Questions