Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8751427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:02:46+00:00 2026-06-13T13:02:46+00:00

I’m not really sure why I am getting this error. The code is meant

  • 0

I’m not really sure why I am getting this error. The code is meant to test palindromes disregarding punctuation.

So here is my code:

            char junk;
            String temp = "";

            for (int i = 0; i < txt.length(); i++)
            {
                junk  = txt.charAt(i);
                if (Character.isLetterOrDigit(txt.charAt(jumk)))
                {
                    temp += junk;
                }
            }
            txt = temp;
            left = 0;
            right = txt.length() -1;

            while (txt.charAt(left) == txt.charAt(right) && right > left)
            {
                left++;
                right--;
            }

java.lang.StringIndexOutOfBoundException : String index out of range 0
at PalindromeTester.main(PalindromeTester.java:35)

and line 35 is as following:

    while (txt.charAt(left) == txt.charAt(right) && right > left)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:02:47+00:00Added an answer on June 13, 2026 at 1:02 pm

    The variable yP is your character at index i, not an index (as you are using it on the line giving you the error). Change that line to:

    if (Character.isLetterOrDigit(yP)) { ...
    

    EDIT FOR THE NEW PROBLEM:

    You don’t need a while loop to check if the user entered nothing, since you don’t want to do something repeatedly in this case (which is what loops are for). Since you only want to do something once, i.e. print out how many palindromes they have found, you can just use an if statement. The structure would look like this:

    do {
        get user input
    
        if they entered the empty string "" {
    
            print out how many palindromes they have found so far
    
        } else { // they must have entered text, so check for palindrome
    
            your normal palindrome checking code goes here
    
        }
    
    } while (your condition);
    

    EDIT 2:

    Try changing

    if (left > right)
    

    to

    if (left >= right)
    

    Since if left==right, that means they are both on the median character in an odd-length string (e.g. the ‘y’ in kayak) which means the string is a palindrome.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
this is what i have right now Drawing an RSS feed into the php,
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.