Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Exception Coming.... Whats wrong with my code....

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation 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:
    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;
    	}
     
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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.
    Improving the world one idiot at a time!

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Whats wrong with my code!!
    By nitwit3 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 22nd, 2011, 11:45 AM
  2. Can't seem to understand whats wrong with my code! Help!!
    By aquir in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 2nd, 2011, 12:06 PM
  3. Whats Wrong with this code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 10:59 PM
  4. Whats wrong with my code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 4th, 2011, 05:34 PM
  5. Whats wrong with my code?
    By mlan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2010, 01:42 PM