Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-05-2010, 01:15 AM
davetheant
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).

• 09-05-2010, 01:39 AM
Fubarable
This little bit of math is unusual:
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!
• 09-05-2010, 02:10 AM
davetheant
Well thanks for that tip. It now looks like this:

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.
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);```
• 09-05-2010, 02:14 AM
Norm
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:
Code:

```          System.out.println("p=" +product);           System.out.println("a=" + a);           System.out.println("b=" + b);```
• 09-05-2010, 02:17 AM
Fubarable
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!
• 09-05-2010, 02:20 AM
davetheant
As far as I can see, when I had it print my values, almost all of them were 8 and 9 for some reason.
• 09-05-2010, 02:21 AM
davetheant
And thanks Fubarable, I hadn't thought of doing it that way. I think I should be able to work this one out :]
• 09-05-2010, 02:23 AM
al_Marshy_1981
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.
• 09-05-2010, 02:32 AM
Fubarable
You're not trying to solve Project Euler problem #4 are you?: Project Euler #4
• 09-05-2010, 02:36 AM
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???????
• 09-05-2010, 02:38 AM
davetheant
Fubarable, in fact, yes I am, and have been since about 8 hours ago :D
Lol. How did you know?
• 09-05-2010, 02:40 AM
Fubarable
Quote:

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.
• 09-05-2010, 02:43 AM
al_Marshy_1981
Quote:

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:
• 09-05-2010, 03:13 AM
davetheant
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.
• 09-05-2010, 03:15 AM
Fubarable
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.
• 09-05-2010, 03:20 AM
al_Marshy_1981
Fubarable take a look at this it makes no sense, it says the strings are equal when they are not:
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;         } }```
• 09-05-2010, 03:24 AM
Norm
Quote:

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.
• 09-05-2010, 03:25 AM
Fubarable
Dang semi-colons! ;)
Code:

```                if(copy.equals(palindrome)); // <=========                 {                         isPalindrome=true;                 }```
• 09-05-2010, 03:27 AM
al_Marshy_1981
Quote:

Originally Posted by Fubarable
Dang semi-colons!
Code:

```                if(copy.equals(palindrome)); // <=========                 {                         isPalindrome=true;                 }```

:) great spot well done, all is well in programming logic land again :D
• 09-05-2010, 03:29 AM
davetheant
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.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last