Results 1 to 6 of 6
  1. #1
    LasseA is offline Member
    Join Date
    Oct 2012
    Posts
    29
    Rep Power
    0

    Default Problems with charAt() in loop

    So I'm trying to make a program which tests if a word is a palindrome or not(like racecar is the same spelled backwards).
    This is my code:

    Java Code:
    import java.util.*;
    public class C4E13 {
    	public static void main (String[] args) {
    		Scanner console = new Scanner(System.in);
    		System.out.println("Enter your word(s) and press enter to test which is palindromes");
    		String word = console.next();
    		printPalindrome(word);
    		
    	}
    	
    	public static void printPalindrome(String word) {
    		int length = word.length();
    		String reverseWord = null;
    		for (int i = 0 ; i <= length ; i++) {
    			char a = word.charAt(length-i);
    			reverseWord = reverseWord+a;
    		}
    			
    		if (word.equals(reverseWord)) {
    			System.out.println("Yes");
    		}
    		
    		else {
    			System.out.println("No");
    		}
    	}
    }
    The error I get when I type in a word is

    Java Code:
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
    	at java.lang.String.charAt(String.java:658)
    	at C4E13.printPalindrome(C4E13.java:15)
    	at C4E13.main(C4E13.java:7)
    Where the number it's out of range is equal to length of the word I type in.

    What is causing the problem? I can't figure it out.

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Problems with charAt() in loop

    Look at line 14 and 15 in your code...

    StringIndexOutOfBoundsException is triggered when you try to read char from position in String that don't exists.
    Last edited by milovan; 01-19-2013 at 02:05 AM.

  3. #3
    LasseA is offline Member
    Join Date
    Oct 2012
    Posts
    29
    Rep Power
    0

    Default Re: Problems with charAt() in loop

    for (int i = 0 ; i <= length-1 ; i++) {
    char a = word.charAt(length-i-1);
    reverseWord = reverseWord+a;
    }
    changed it to the code above, and it seems to work now thanks :)
    Well that part of the code seems to work, that's it, because if I type in "racecar" it says no, which it shouldn't

    My guess is there's something that needs to change about this piece of code
    reverseWord = reverseWord+a;

  4. #4
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default Re: Problems with charAt() in loop

    Just for the fun of it: print out reverseWord just before you test it (after the loop has finished).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    LasseA is offline Member
    Join Date
    Oct 2012
    Posts
    29
    Rep Power
    0

    Default Re: Problems with charAt() in loop

    Thanks for answers guys!
    I figured out that I just had to write "" instead of null, I thought null would be the same as writting nothing.
    So I tried printing reverseWord after loop as JosAH suggested, and I found mistake in writting null.

Similar Threads

  1. Loop problems
    By amzers in forum New To Java
    Replies: 23
    Last Post: 08-16-2011, 08:18 PM
  2. Problems with While loop execution
    By Kevinius in forum New To Java
    Replies: 12
    Last Post: 03-03-2011, 08:35 AM
  3. Loop problems
    By jim01 in forum New To Java
    Replies: 3
    Last Post: 10-18-2010, 01:49 AM
  4. Newbie having problems with for loop
    By Dannii in forum New To Java
    Replies: 4
    Last Post: 04-14-2009, 12:52 AM
  5. Problems with while loop
    By Albert in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 08:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •