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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:10:21+00:00 2026-06-04T13:10:21+00:00

after the previous fix i now have a new issue. Got advised to ask

  • 0

after the previous fix i now have a new issue. Got advised to ask a new question, so here it is!
It still force closes, but now on a new line.

public void AI(String text) {
    // initiate new lists to be on the safe side
    List<String>    indexer = new ArrayList<String>();
    List<String>    sentence = new ArrayList<String>();
    List<String>    explode = new ArrayList<String>();
    List<String>    ary = new ArrayList<String>(); 

    explode = Arrays.asList(text.split(" "));  

    // initiate randint and trigger variable
    Random rand = new Random();
    int randint = rand.nextInt(explode.size());

    // initiate the trigger variable
    String trigger = explode.get(randint); 

    //check if word exists in database and add if it not.
    int i = 0;
    for(String word : explode) {
        if(common.get(word) == null) {
            words.add(word);
            common.put(word, 1);
            context.put(word, explode);
            pos.put(word, i);
            length.put(word, explode.size());



            if(word.equals(trigger)) {
                ary = new ArrayList<String>(explode);
            }
        }else{

            common.put(word, common.get(word)+1 );  
        }

        i++;
    }



    // fill the arraylist sentence with words to be used, some in context, some random from the database.
    for(int i2 = 0; i2 < length.get(trigger); i2++ ) {

        randint = rand.nextInt(length.get(trigger));

        if(randint < length.get(trigger)/2) {
            //
            if(ary.get(i2)!=null) {
                sentence.add(ary.get(i2));
            }
        }else{
            sentence.add(words.get(rand.nextInt(words.size())));
        }

    }    

    // use to the pos-hashmap to check at which index the word was detected at, if not  place it at the deliberate index.
    for(int i3 = 0; i3 < sentence.size(); i3++) {
        if(sentence.get(i3)!=null) {    
            indexer.add(pos.get(sentence.get(i3)),   sentence.get(i3)); 
        }
    }

    // compose the final string that is to be passed to the speak function
    for(int i4 = 0; i4 < indexer.size(); i4++) {
        say = say + indexer.get(i4)+" ";    
    }   

    // pass the string to the speak function
    mTts.speak(say, TextToSpeech.QUEUE_FLUSH, null);     


    // removing final string to be ready for next iteration
    say = "";



    // end of AI stuff          
}   

Variables:

    // arraylists
public List<String> explode = new ArrayList<String>();
public List<String> words = new ArrayList<String>();
public List<String> sentence = new ArrayList<String>();
public List<String> indexer = new ArrayList<String>();
// hashmaps
public Map<String, Integer> common = new HashMap<String, Integer>();
public Map<String, List<String>> context = new HashMap<String, List<String>>();
public Map<String, Integer> pos = new HashMap<String, Integer>();
public Map<String, Integer> length = new HashMap<String, Integer>();
 //strings
    public String say;

And logcat:

05-26 19:24:42.926: W/dalvikvm(543): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-26 19:24:42.926: E/AndroidRuntime(543): Uncaught handler: thread main exiting due to uncaught exception
05-26 19:24:42.956: E/AndroidRuntime(543): java.lang.IndexOutOfBoundsException: Invalid location 0, size is 0
05-26 19:24:42.956: E/AndroidRuntime(543):  at java.util.ArrayList.get(ArrayList.java:341)
05-26 19:24:42.956: E/AndroidRuntime(543):  at sarah.namespace.SarahActivity.AI(SarahActivity.java:186)

Second logcat:

 05-26 21:38:44.555: W/dalvikvm(571): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-26 21:38:44.555: E/AndroidRuntime(571): Uncaught handler: thread main exiting due to uncaught exception
05-26 21:38:44.575: E/AndroidRuntime(571): java.lang.IndexOutOfBoundsException
05-26 21:38:44.575: E/AndroidRuntime(571):  at java.util.ArrayList.add(ArrayList.java:145)
05-26 21:38:44.575: E/AndroidRuntime(571):  at sarah.namespace.SarahActivity.AI(SarahActivity.java:188)

What the logcats are screaming at:

First:

sentence.add(words.get(rand.nextInt(words.size())));

Second:

for(int i3 = 0; i3 < sentence.size(); i3++) {
                                            if(sentence.get(i3)!=null) {    
                                            indexer.add(pos.get(sentence.get(i3)), sentence.get(i3));   
                                                                       }
                                            }

I would be very happy if someone could help me beat this riddle again!

  • 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-04T13:10:23+00:00Added an answer on June 4, 2026 at 1:10 pm

    Hmm, I am uncertain about which lines are 186 and 188 (the two that throw the OutOfBounds exceptions). But looking at this loop, I have some suggestions:

    for(int i2 = 0; i2 < length.get(trigger); i2++ ) {
    

    From above we notice that length.get(trigger) is always set to explode.size() and we can see that explode.size() never changes. We could check against explode.size() instead or use a for-each loop again. Carrying on:

        randint = rand.nextInt(length.get(trigger));
    
        if(randint < length.get(trigger)/2) {
    

    This looks like you want a 50/50 chance of: adding the original word to sentence or

            if(ary.get(i2)!=null) {
                sentence.add(ary.get(i2));
            }
        }else{
            sentence.add(words.get(rand.nextInt(words.size())));
        }
    }    
    

    … a random word to sentence.

    Also I beleive this could throw an exception:

            if(ary.get(i2)!=null) {
                sentence.add(ary.get(i2));
    

    because you are checking the validity of i2 against length.get(trigger) not ary.size(). Now remember that ary is simply a copy of explode.

    So let’s condense this loop to:

    for(String word : explode) {
        if(rand.nextInt(1)) // 50/50 chance of being false or true (0 or 1)
            sentence.add(word);
        else
            sentence.add(words.get(rand.nextInt(words.size())));
    }
    

    Take a look at the remaining two loops, if you apply a similar for-each loop you will be able to condense and expedite these as well. I don’t know the rest of the app’s code, but you might be able to remove a few Lists and Maps entirely. Cheers!

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

Sidebar

Related Questions

The VXML application I mentioned in a previous question is now in testing. After
I didn't get any more responses after the previous question I had. I got
I hate to ask the same question others have asked in Stackoverflow, but I
Any advice on how to fix this issue I have, or a better implementation
Is there a way i can uninstall (and dont upgrade after) a previous version
I found many way for redirect to previous page after login and example :
After from processing i am sending the user on the previous page using: header('Location:
I can't figure out why the next page/previous page buttons stop working after a
After loosing much sleep I still cannot figure this out: The code below (its
After many cases of trial and error, I have concluded that the only solution

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.