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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:20:26+00:00 2026-06-04T17:20:26+00:00

im new to java, coming from c++, and to quickly learn different practices im

  • 0

im new to java, coming from c++, and to quickly learn different practices im converting my chatbot to a java android app. This function right here force-closes if it gets more than 3 different words, i have no idea why. I would be thrilled if someone could help me beat this riddle!

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.
for(int i = 0; i < explode.size(); i++) {
    String word = explode.get(i);   

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());

                         }else{
        // increase the weight of common if the word repeats
                         common.put(word, common.get(word)+1 );
                                     }
                                  }
     //check if context with index set as trigger is empty, if not, copy the   arraylist to the ary variable
     if(!context.get(trigger).isEmpty()) {
            Collections.copy( ary, context.get(trigger));}


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

LogCat:

05-26 17:40:08.266: E/AndroidRuntime(490): Uncaught handler: thread main exiting due to  uncaught exception
05-26 17:40:08.276: E/AndroidRuntime(490): java.lang.ArrayIndexOutOfBoundsException
05-26 17:40:08.276: E/AndroidRuntime(490):  at  java.util.Collections.copy(Collections.java:1593)
05-26 17:40:08.276: E/AndroidRuntime(490):  at sarah.namespace.SarahActivity.AI(SarahActivity.java:163)

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;

The line Logcat is pointing at:

if(!context.get(trigger).isEmpty()) {
            Collections.copy( ary, context.get(trigger));}
  • 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-04T17:20:28+00:00Added an answer on June 4, 2026 at 5:20 pm

    I haven’t pinpointed the problem, but I believe this alternate approach is faster and should avoid it. (Honestly, your code crashes on the fourth word? That’s just weird. I believe there is more to it… Anyway)

    In Java for-each loops are faster and simpler, so:

    // check if word exists in database and add if it not.
    int i = 0;
    for(String word : explode) {
        if(common.get(word) == null) {
            if(word.equals(trigger)) {
                ary = new ArrayList<String>(explode);
            }
    
        ...
        i++;
    }
    

    Also if trigger does not exist in context then null is returned, not an empty List, so you might be able to do a simple change:

    if(context.get(trigger) != null && !context.get(trigger).isEmpty()) {
        Collections.copy(ary, context.get(trigger));
    }
    

    But this should throw a NullPointerException not an out of bounds… I still don’t know why the fourth word is fatal. Hope this helps.

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

Sidebar

Related Questions

I am new to Java coming from a PHP background so sorry if this
Another question about my implementation. I am fairly new to java/android coming from a
I'm brand new to java, coming from a ruby world. One thing I love
Note: I'm relatively new to Objective-C and am coming from Java and PHP. Could
I'm very new to Python (I'm coming from a JAVA background) and I'm wondering
Coming from Java background I am guessing this is expected. I would really love
I'm new to Python... and coming from a mostly Java background, if that accounts
I am new to coding in Objective-C, but am coming from the Java world
First off my background: I'm new to Java coming over from Ruby. If that
I am fairly new to C# coming from Java, and I'm wondering if there's

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.