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 9239241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:56:32+00:00 2026-06-18T07:56:32+00:00

So for example, I have this string: 0no1no2yes3yes4yes The first 0 here should be

  • 0

So for example, I have this string:

0no1no2yes3yes4yes

The first 0 here should be removed and used an an index of array. I am doing so by this statement:

string = string.replaceFirst(dataLine.substring(0, 1), "");

However, when I have say this string:

10yes11no12yes13yes14no

My code fails, since I want to process the 10 but my code extracts just the 1.

So in sort, single digits work fine, but double or triple digits cause IndexOutOfBound Error.

Here’s the code: http://pastebin.com/uspYp1FK

And here’s some sample data: http://pastebin.com/kTQx5WrJ

Here’s the output for the sample data:

Enter filename: test.txt
Data before cleanUp: {"assignmentID":"2CCYEPLSP75KTVG8PTFALQES19DXRA","workerID":"AGMJL8K9OMU64","start":1359575990087,"end":"","elapsedTime":"","itemIndex":0,"responses":[{"jokeIndex":0,"response":"no"},{"jokeIndex":1,"response":"no"},{"jokeIndex":2,"response":"yes"},{"jokeIndex":3,"response":"yes"},{"jokeIndex":4,"response":"yes"}],"mturk":"yes"},
Data after cleanUp: 0no1no2yes3yes4yes
Data before cleanUp: {"assignmentID":"2118D8J3VE7W013Z4273QCKAGJOYID","workerID":"A2P0GYVEKGM8HF","start":1359576154789,"end":"","elapsedTime":"","itemIndex":3,"responses":[{"jokeIndex":15,"response":"no"},{"jokeIndex":16,"response":"no"},{"jokeIndex":17,"response":"no"},{"jokeIndex":18,"response":"no"},{"jokeIndex":19,"response":"no"}],"mturk":"yes"},
Data after cleanUp: 15no16no17no18no19no
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
    at java.lang.String.substring(String.java:1907)
    at jokes.main(jokes.java:34)

Basically, what the code is supposed to do is strip off the data into strings as shown above, and then read the number, and if it’s followed by yes increase it’s index’s value in dataYes, or if followed by no increase value in dataNo. Makes sense?

What can I do? How can I make my code more flexible?

  • 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-18T07:56:33+00:00Added an answer on June 18, 2026 at 7:56 am

    An alternative, more specific attempt: –

        String regex = "^(\\d+)(yes|no)";
        String myStr = "10yes11no";
    
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(myStr);
    
        while (m.find())
        {
            String all = m.group();
            String digits = m.group(1);
            String bool = m.group(2);
    
            // do not try and combine the next 2 lines ... it doesn't work!
            myStr = myStr.substring(all.length());
            m.reset(myStr);
    
            System.out.println(String.format("all = %s, digits = %s, bool = %s", all, digits, bool));
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this string: EXAMPLE|abcd|[!PAGE|title] I want to split it like this: Array (
Here is my issue. I have this: String data = java.awt.Color[r=168,g=228,b=160] //example this changes.
Basically I have this string for example $str = 'По, своей 12' I need
For example, I have this string that could change anytime I only want the
I have for example this string: iCanSeeBluePeople and I need it to separate it
I have a string as const char *str = Hello, this is an example
For example, I have a string variable named str. This str has a value
For example I have this string of 2 json objects: {"glossary": {"title": "example glossary"},
I have this string for example : one two three four START four five
I have this string(It's example only): $string='cat-cat.png-0,dog-dog.jpg-0,phone-nokia.png-1,horse-xyz.png-0'; And code to remake this string $string=explode(,,

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.