Exception Coming.... Whats wrong with my code....
Hi all,
I am trying to write a code to find out the palindrome words from a sentence. Palindrome words are those words which remains same while reading from left to write or right to left like LEVEL, MADAM, MOM, DAD.
My programming is giving output but with this exception
"Exception in thread "main" java.lang.StringIndexOutOfBoundException: String index out of range: 43"
Kindly help me to come out of this issue.... I don't want to use "Split" function.
Here is my code:
Code Java:
public class Sentence
{
public static void main(String [] args)
{
String str = "HELLO ROTATOR WHERE MADAM IS LEVEL AND POP.";
int i=0;
int j=0;
int s=0;
String word;
String temp;
String str2;
int len=str.length();
for(i=s;i<str.length()-1;)
{
str2="";
while((str.charAt(j)!=' ') && (str.charAt(j)!='.'))
{
str2=str2+str.charAt(j);
j++;
}
s=++j;
word = reverse(str2);
word=word.intern();
str2=str2.intern();
if(str2==word)
{
System.out.println(str2 +"is palindrome");
}
}
}
public static String reverse(String temp)
{
String str3 = "";
for(int k=temp.length()-1;k>=0;k--)
{
str3= str3+temp.charAt(k);
}
return str3;
}
}
Re: Exception Coming.... Whats wrong with my code....
Please post your code using code tags. See the BB code link at the bottom of this page. This will avoid your code being transmogrified into a smiley. Also post the full and exact error message. Somewhere in the stack trace it will indicate on which line of your code the error occurs. Please indicate which line this is as well.
Re: Exception Coming.... Whats wrong with my code....
I have added the highlight tags for you. Please see my signature on how to do this in the future.
Have you Googled this Exception? It's pretty self explanatory.
I suggest adding print statements to see what is going on with your loops.
Debugging with System.out.println