1. Member
Join Date
Sep 2010
Posts
47
Rep Power
0

Hello everyone. In my program i have a section that checks if a number is a palindrome (can be read forwards or backwards). This is my code:
if ((s.charAt(s.length() - x - 1)) == (s.charAt(s.length() - s.length() + x)))
and x is declared at 0. I get an error that says: java.lang.StringIndexOutOfBoundsException: String index out of range: -1 along with some other stuff that I don't think is really that important. Can someone help me in figuring out this error? I don't know how the index could go below 0, when I'm never subtracting x (except for right there).

2. This little bit of math is unusual:
Java Code:
s.length() - s.length() + x

as your first term is being canceled by the second term.

I think that we'll need to see more code, such as your for loop. If you do post this, please use code tags (please see the link in my signature for help with this).

Much luck and welcome to the forum!

3. Member
Join Date
Sep 2010
Posts
47
Rep Power
0
Well thanks for that tip. It now looks like this:

Java Code:
if ((s.charAt(s.length() - 1 - x)) == (s.charAt(x)))
but I still get the same error and the same output. As for the loop, I'm just using a while loop.
I declared boolean c to be true, and int's a & b to equal 998 and 999, respectively. Product is an int as well. I'm pretty sure I have some unnecessary stuff in there, but it was just to help my try to figure out the error. Thanks again.
Java Code:
while (c)
{
product = a*b;
s = String.valueOf(product);
if ((s.charAt(s.length() - x - 1)) == (x))
{
x++;
product = a*b;
s = String.valueOf(product);
if (x == s.length())
{
product = a*b;
System.out.println(product);
System.out.println(a);
System.out.println(b);
}
}
else if ((b - a) == 1)
{
x = 0;
a -= 1;
b = 999;
product = a*b;
s = String.valueOf(product);
}
else if ((b - a) > 1)
{
x = 0;
b -= 1;
product = a*b;
s = String.valueOf(product);

4. If you don't see what is wrong with your index's value, you need to do some debugging work by extracting all the components of the expression and print them out to see why it generates a value of -1.

If the println()s are for debugging a better format is:
Java Code:
System.out.println("p=" +product);
System.out.println("a=" + a);
System.out.println("b=" + b);
Last edited by Norm; 09-05-2010 at 02:16 AM.

5. Sorry, but it's hard to tell what it is you're trying to do here as there is a lot of redundancy, a lot of unexplained and uninitialized variables (at least to our eyes, such as the cryptic "x"). If you want to look for a palindrome of a String, and they're all numeric digits (no need to worry about spaces or capitalization) you could simply reverse the String using say a StringBuilder and comparing the resulting String with the original. If you need more help (at least for me), I'd request more explanation, more code (I suppose) and variable names that make their intent obvious -- self-commenting variable names.

Luck!

6. Member
Join Date
Sep 2010
Posts
47
Rep Power
0
As far as I can see, when I had it print my values, almost all of them were 8 and 9 for some reason.

7. Member
Join Date
Sep 2010
Posts
47
Rep Power
0
And thanks Fubarable, I hadn't thought of doing it that way. I think I should be able to work this one out :]

8. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
11
I agree with fubarable you got some what I would call (mystical) variables floating around your posted code, the problem itself is not a hard one but you could be making it so and its hard to tell when variables such as 'x' just "show up" in your code.

9. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
11
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

erm the largets 2 digits should be 99X99=? surely???????

10. Member
Join Date
Sep 2010
Posts
47
Rep Power
0
Fubarable, in fact, yes I am, and have been since about 8 hours ago :D
Lol. How did you know?

11. Originally Posted by al_Marshy_1981
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

erm the largets 2 digits should be 99X99=? surely???????
to Al Marshy: 99*99 = 9801, and 9801 is not a palindromic number.

To the OP, it just looked somewhat familiar, that's all.

12. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
11
Originally Posted by Fubarable
to Al Marshy: 99*99 = 9801, and 9801 is not a palindromic number.

To the OP, it just looked somewhat familiar, that's all.
:) of course, thanks fubarable, maddening how your brain can turn to mush after so many hours :mad:

13. Member
Join Date
Sep 2010
Posts
47
Rep Power
0
Thanks for everyone's help. I now have the program set up with a string buffer instead of that messy code earlier. I am trying to have: if ( stringbuffer = stringbuffer.reverse() ), but the code doesn't seem to work. Upon printing the two values they are the same. Thanks in advance.

14. You would need to compare the original String vs. the String returned by the reversed String buffer, and would need to use the equals method. I honestly don't know if the string methods would be more efficient than numeric methods though.

15. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
11
Fubarable take a look at this it makes no sense, it says the strings are equal when they are not:
Java Code:
public class Palindrome {

public static void main(String[] args) {

String name="jimmy";
boolean isPally=isPalindrome(name);
System.out.print(isPally);

}

public static boolean isPalindrome(String palindrome){
String copy="";
boolean isPalindrome=false;
for(int i=palindrome.length()-1;i>=0;i--)
{
copy+=palindrome.charAt(i);
}
if(copy.equals(palindrome));
{
isPalindrome=true;
}
return isPalindrome;
}
}

16. it says the strings are equal when they are not:
Can you add a println() statement to the code that shows both strings? Put them inside delimiters (">" & "<") to show any spaces that could be there.

17. Dang semi-colons! ;)
Java Code:
if(copy.equals(palindrome)); // <=========
{
isPalindrome=true;
}

18. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
11
Originally Posted by Fubarable
Dang semi-colons!
Java Code:
if(copy.equals(palindrome)); // <=========
{
isPalindrome=true;
}
:) great spot well done, all is well in programming logic land again :D

19. Member
Join Date
Sep 2010
Posts
47
Rep Power
0
Thanks everyone. I think I got everything right now, but when I run, I get a message saying 'Your program ran out of memory'. I'm on my laptop. Should I go on my desktop? It has a lot more memory and an x64 OS.

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•