Hi,
I'm creating a palindrome program. So far, I got that part. Now, I need to find out if the length of the integer (string) is less than 5 digits. If the length is less than 5, then don't proceed with the program. Please give your suggestions. Basically, what I'm doing is converting the Int to String and then finding the length. Thanks!
I tried the following:
if ( s.length() >= 5 ) {
// Proceed
}
and got this:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at _uesJavaRun.main(_uesJavaRun.java:29)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.charAt(String.java:558)
at palindrome.main(palindrome.java:26)
... 5 more
This is my code so far:
import java.util.Scanner;public class palindrome
{
public static void main (String args[])
{Scanner input = new Scanner (System.in );
int number;
String len, size;System.out.print( "Enter a 5-digit integer: " );
number = input.nextInt();len = number + "";
// print error message if the length is less than 5 digits
System.out.println ("Your input is not a 5-digit long. Try again.");
}
}