Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: String index out of bounds error 5???

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default String index out of bounds error 5???

    hey, im working on a program that tests to see if a string is a palindrome or not. It has to remove all characters and spaces.
    Heres the program:
    import java.util.*;
     
    public class PalindromeTester
    {
    	public static void main(String[]args)
    	{
    	String str,another="y";
    	int left,right;
     
     
    	Scanner scan=new Scanner(System.in);
     
    		while(another.equalsIgnoreCase("y"))
    		{
    		System.out.println("enter a potential palindrome: ");
    		str=scan.nextLine();
     
    		str=str.replaceAll("!"," ");
    		str=str.replaceAll("."," ");	
     
     
     
                   right=str.length();
    		left=0;
     
    		while(str.charAt(left)== str.charAt(right) && left < right)
    		{
     
     
     
    		left++;
    		right--;
     
    		}
     
    		System.out.println();
     
    		if(left<right)
    			System.out.println("That string is NOT a palindrome. ");
     
    		else 
    			System.out.println("That string IS a palindrome");
     
    		System.out.println();
    		System.out.println("Test another palindrome?(y/n)");
    		another=scan.nextLine();
    		}
     
    	}
    }

    It compiles fine, but when i run the program i get this error:

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
    at java.lang.String.charAt(String.java:686)
    at PalindromeTester.main(PalindromeTester.java:26)

    What does this mean? and one other question. If i use the string.replaceAll() method, can i replace it with nothing by doing this string.replaceAll("(puntuationmarkshere),"");
    Last edited by helloworld922; September 26th, 2010 at 06:26 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: String index out of bounds error 5???

    It means your Array is only 5 items long, and you are attempting to access Index 5. Since Indexes start at 0, your index range is: 0-4.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String index out of bounds error 5???

    thank you! i just fixed it im starting to understand these error messages better and better. now what about the second question?

  4. #4
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: String index out of bounds error 5???

    Yes you can replace the string with ""

Similar Threads

  1. index of array
    By nasi in forum Java Theory & Questions
    Replies: 1
    Last Post: April 12th, 2010, 02:39 AM
  2. [SOLVED] Using class implicit toString() for array index
    By Quetzalma in forum Java Theory & Questions
    Replies: 2
    Last Post: February 3rd, 2010, 05:04 PM
  3. Index Out Of Bounds
    By chronoz13 in forum Collections and Generics
    Replies: 1
    Last Post: December 28th, 2009, 12:19 PM
  4. Having problems with String out of bounds errors
    By Bill_H in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2009, 02:47 PM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM