Results 1 to 3 of 3
- 11-03-2012, 06:12 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 1
- Rep Power
- 0
Palindrome testing program. String error
I keep getting an error saying: Exception in thread"main" java.lang.StringIndexOutOfBoundsException: String index out of range: 109
at java.lang.String.charAt(unknow source)
at test.main(test.java:23)
Here is my code:
I don't seem to get the problem if the string is only one char long. But I can't seem to find my mistake.Java Code:import java.util.Scanner; public class test { public static void main (String[] args) { Scanner input = new Scanner(System.in); String palTest = input.next(); int test = palTest.length()-1; char firstChar, lastChar; char first = palTest.charAt(0); char last = palTest.charAt(test); while (first<last) { firstChar = palTest.charAt(first); lastChar = palTest.charAt(last); if(firstChar == lastChar) { first++; last--; } } System.out.println(); if (first < last) System.out.println("k"); else System.out.println("wtf"); } }
*edit* It seems to work with numbers only actually >_>Last edited by Hotsawwce; 11-03-2012 at 06:20 PM.
- 11-03-2012, 07:08 PM #2
Member
- Join Date
- Sep 2012
- Posts
- 34
- Rep Power
- 0
Re: Palindrome testing program. String error
here you use first and last, which are CHARS, not int location in the string..Java Code:firstChar = palTest.charAt(first); lastChar = palTest.charAt(last);
think the problem starts here.
- 11-03-2012, 11:36 PM #3
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Similar Threads
-
Palindrome Program
By e.cummins95 in forum New To JavaReplies: 7Last Post: 03-09-2012, 12:59 AM -
error when testing program (for loop with nested if statement))
By leoshiner in forum New To JavaReplies: 7Last Post: 05-03-2011, 06:41 PM -
Palindrome program
By trinity in forum New To JavaReplies: 4Last Post: 04-16-2011, 03:22 AM -
How to count number of palindrome in a string??
By i4ba1 in forum Advanced JavaReplies: 5Last Post: 04-12-2011, 12:58 PM -
Palindrome program help
By Nel in forum New To JavaReplies: 1Last Post: 03-03-2011, 03:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks