Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default String index out of range: -1 Please Help

    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).

    Thanks in advance.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #3
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    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. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    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. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #6
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    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. #7
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    And thanks Fubarable, I hadn't thought of doing it that way. I think I should be able to work this one out :]

  8. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  10. #10
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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???????

  11. #11
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    Fubarable, in fact, yes I am, and have been since about 8 hours ago :D
    Lol. How did you know?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    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.

  13. #13
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by Fubarable View Post
    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:

  14. #14
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    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.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  16. #16
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

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

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    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.

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

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

  19. #19
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by Fubarable View Post
    Dang semi-colons!
    Java Code:
    		if(copy.equals(palindrome)); // <=========
    		{
    			isPalindrome=true;
    		}
    :) great spot well done, all is well in programming logic land again :D

  20. #20
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    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 LastLast

Similar Threads

  1. Replies: 4
    Last Post: 08-19-2010, 01:06 AM
  2. find index of string in another string
    By Sdannenberg3 in forum New To Java
    Replies: 4
    Last Post: 03-04-2010, 10:14 AM
  3. Find index position of every word in a String
    By pentace in forum New To Java
    Replies: 6
    Last Post: 06-28-2009, 08:26 PM
  4. Range within an Array
    By End in forum New To Java
    Replies: 6
    Last Post: 04-18-2009, 06:53 PM
  5. Noob here, trying to access index in string
    By madmax2006 in forum New To Java
    Replies: 14
    Last Post: 03-08-2009, 05:08 PM

Tags for this Thread

Posting Permissions

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