Results 1 to 6 of 6
Thread: Problems with charAt() in loop
- 01-19-2013, 01:04 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 29
- Rep Power
- 0
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"); } } }
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)
What is causing the problem? I can't figure it out.
- 01-19-2013, 01:38 AM #2
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 311
- Rep Power
- 11
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.
- 01-19-2013, 02:10 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 29
- Rep Power
- 0
Re: Problems with charAt() in loop
for (int i = 0 ; i <= length-1 ; i++) {
char a = word.charAt(length-i-1);
reverseWord = reverseWord+a;
}
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 codereverseWord = reverseWord+a;
- 01-19-2013, 11:14 AM #4
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 311
- Rep Power
- 11
Re: Problems with charAt() in loop
- 01-19-2013, 12:40 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 01-19-2013, 09:48 PM #6
Member
- Join Date
- Oct 2012
- Posts
- 29
- Rep Power
- 0
Similar Threads
-
Loop problems
By amzers in forum New To JavaReplies: 23Last Post: 08-16-2011, 08:18 PM -
Problems with While loop execution
By Kevinius in forum New To JavaReplies: 12Last Post: 03-03-2011, 08:35 AM -
Loop problems
By jim01 in forum New To JavaReplies: 3Last Post: 10-18-2010, 01:49 AM -
Newbie having problems with for loop
By Dannii in forum New To JavaReplies: 4Last Post: 04-14-2009, 12:52 AM -
Problems with while loop
By Albert in forum New To JavaReplies: 2Last Post: 07-04-2007, 08:19 AM
Bookmarks