Forum Stats

  • 3,874,157 Users
  • 2,266,675 Discussions
  • 7,911,745 Comments

Discussions

String Index Out Of Bounds Exception

807603
807603 Member Posts: 44,391
edited Jan 31, 2008 5:10AM in Java Programming
import java.io.*;
public class Array
{
public static void main() throws Exception
{
System.out.println("Enter a word:");
BufferedReader br= new BufferedReader (new InputStreamReader(System.in));
String ar1=br.readLine();
int n=ar1.length();
for(int i=0; i<n+1; i++)
{
if(ar1.charAt(i)=='a')
{
int sum=0;
sum=sum+1;
}
if(ar1.charAt(i)=='e')
{
int sum=0;
sum=sum+1;
}
if(ar1.charAt(i)=='i')
{
int sum=0;
sum=sum+1;
}
if(ar1.charAt(i)=='o')
{
int sum=0;
sum=sum+1;
}
if(ar1.charAt(i)=='u')
{
int sum=0;
sum=sum+1;
}
else
{
int num=0;
num=num+1;
}
for(int j=n; j>=0; j--)
{
System.out.print(ar1.charAt(j));
}
}
}
}
it is a program to count the number of vowels and consonants in a string entered by the user and then inverts it to check if it is a palindrome.

everytime i execute the program, it gives me a String Index Out Of Bounds Exception... what do i do?? the highlighted line is the one which has the exception...

thanks...

Message was edited by:
emii_dan

Comments

  • 800282
    800282 Member Posts: 12,480 Green Ribbon
    edited Aug 26, 2007 8:11AM
    From a String of length n, the characters are accessible from indexes[b] 0 to n-1. If you try to get a character with a negative index or an index >= n, an IndexOutOfBoundsException will be thrown.
  • 800345
    800345 Member Posts: 4,981
    String indexes like arrays, are 0 indexed. You have
    int n = string.length();
    ...
    for (int j=n ...) {
      System.out.println(string.charAt(j););
    The first time through that loop, it will ask for string.charAt(n) which is the length of the String. But the String positions are 0 indexed, so there is no char[n], the string only gets to char[n-1].

    So you should do:
    int n = string.length();
    ...
    for (int j=n-1 ...) {
      System.out.println(string.charAt(j););
    Also, giving variables more meaningful names, along with formating your code so it is readable helps debugging.
  • 807603
    807603 Member Posts: 44,391
    the highlighted line is the one which has the
    exception...
    I test your code and made some modification that the
    guys suggested.

    That is not your only error also this part
    for(int i=0; i<n+1; i++) // n+1
       if(ar1.charAt(i)=='a') // when it reaches here IndexOutOfBounds
    I think you can solve this problem by reading the suggested solution.
  • 807603
    807603 Member Posts: 44,391
    thanks guys!!! that worked...
  • 807603
    807603 Member Posts: 44,391
    hi guys... uhmmm. am just new here...
    i have a problem in our class.i need the user to input words then my proram will invert the words and count how many vowels, consonants and capital letters there are.
    i am really gratefulfor your help. thanks and god bless.
  • JoachimSauer
    JoachimSauer Member Posts: 4,780
    edited Jan 31, 2008 5:10AM
    1.) Welcome to the forum
    2.) Don't just add your question to an existing (and very old) post, but create a new one for your question.
    3.) Break the problem down into seperate steps and attempt to solve each one of them (read some basic tutorials http://java.sun.com/docs/books/tutorial/ to get you started).
    4.) if you've got a specific problem and/or question, then come back and ask about that.
This discussion has been closed.