Results 1 to 6 of 6
Thread: Problems with charAt() in loop
- 01-19-2013, 12:04 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 28
- 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:
The error I get when I type in a word isJava 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"); } } }
Where the number it's out of range is equal to length of the word I type in.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, 12:38 AM #2
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
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 01:05 AM.
- 01-19-2013, 01:10 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 28
- Rep Power
- 0
Re: Problems with charAt() in loop
changed it to the code above, and it seems to work now thanks :)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, 10:14 AM #4
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Re: Problems with charAt() in loop
- 01-19-2013, 11:40 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-19-2013, 08:48 PM #6
Member
- Join Date
- Oct 2012
- Posts
- 28
- Rep Power
- 0
Similar Threads
-
Loop problems
By amzers in forum New To JavaReplies: 23Last Post: 08-16-2011, 07:18 PM -
Problems with While loop execution
By Kevinius in forum New To JavaReplies: 12Last Post: 03-03-2011, 07:35 AM -
Loop problems
By jim01 in forum New To JavaReplies: 3Last Post: 10-18-2010, 12:49 AM -
Newbie having problems with for loop
By Dannii in forum New To JavaReplies: 4Last Post: 04-13-2009, 11:52 PM -
Problems with while loop
By Albert in forum New To JavaReplies: 2Last Post: 07-04-2007, 07:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks