The Artima Developer Community
Sponsored Link

Java Answers Forum
problem with this function

2 replies on 1 page. Most recent reply: Oct 8, 2002 11:37 PM by Sridhar

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Cshah

Posts: 16
Nickname: tkc204
Registered: Oct, 2002

problem with this function Posted: Oct 8, 2002 7:42 PM
Reply to this message Reply
Advertisement
i get three errors on this function i need help

public static void palinDrome(String word)
{

String word1;

word1 = word.toLowerCase();


for ( int x = word.length()-1; x < word.length(); x--)
{
String word2;
word2 = word.substring(x, x+1);
System.out.print(word2);

}
System.out.println(" ");

if (word1.equals(word2))
{
System.out.println("True, The palindrome of " + word1 + " is " +

word2);
}
else
{
System.out.println(" False, Not a palindrome of " + word);

}

}


mike

Posts: 2
Nickname: concept
Registered: Oct, 2002

Re: problem with this function Posted: Oct 8, 2002 10:34 PM
Reply to this message Reply
What are the errors?

testing..



public void main(String args[]){



}

Sridhar

Posts: 3
Nickname: sweetsri
Registered: Oct, 2002

Re: problem with this function Posted: Oct 8, 2002 11:37 PM
Reply to this message Reply
Hey u have to declare the word2 inside the for loop so u the scope of word2 will be confined to that loop u cannot use it outside of that loop... okeee........

so declare word2 int the function scope...

and u have used substring function.. which goes out of range... and throws StringIndexOutOfRangeException....
substring function takes two parameters are .. one is starting position in the string and no of characters to be retrieved... okeee
u'r give start position as stringlength-1 and retrieving stringlength + 1 no of characters...

Flat View: This topic has 2 replies on 1 page
Topic: new to java Previous Topic   Next Topic Topic: MultiLinked List

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use