0

, I'm comparing one arrayList with another arrayList, if elements that are "contained" in both i would like to attach the element in the second arraylist to the element in the first along with "== neg==". The problem is I'm creating a new element in the first Arraylist instead of appending it to the element. I'm new to programming any assistance would be appreciated!!

 public static List<String> neg_compare(  List<String>tagged, List<String>negative_words, List<String>total_compare )
        {
        //int pos_sentiment = 0;
        //int pos_count = 0;
        int total_tweets = 0;
        int neg_count = 0;


                for(int j =0 ; j < tagged.size(); j++)
                {
                    total_tweets ++;
                    total_compare.add(tagged.get(j));

                    for(int  k = 0; k < negative_words.size(); k++)
                    {
                        if(tagged.get(j).contains(negative_words.get(k)))
                        {

                            //pos_count ++;

                            total_compare.add( "   == neg == " + negative_words.get(k) +"\n");

                        }
                    }
                    System.out.print(total_compare + "\n");
                    System.out.print(total_tweets);
                }
                    return total_compare;
            }
}
8
  • Did u try this link? Commented Mar 11, 2014 at 9:12
  • Please put more effort into formatting your code before you post - it's all over the place at the moment. Look at the preview when you're writing your post, and only submit the post when it looks reasonable. It would also help massively if you'd give a short but complete program which demonstrates the problem. (Say what you expected to happen vs what actually happened.) Commented Mar 11, 2014 at 9:14
  • You are not allowed to make changes to an arraylist inside of normal loops. To be able to add, remove or modify an ecisting element you should be using an Iterator. Information on how to use them in your example can be found at the Java Tutorial Docs: docs.oracle.com/javase/tutorial/collections/interfaces/… I hope this information can help. Commented Mar 11, 2014 at 9:16
  • 1
    You said that " I'm creating a new element in the first Arraylist instead of appending it to the element" - this sounds as if it might be worth mentioning that you can not modify strings. (But maybe I misunderstood something here - your intention is not very clear) Commented Mar 11, 2014 at 9:24
  • @Marco13: That is the way I know it, however, I did a quick test and I did not get any exceptions. I am not sure if it is because I am not messing around with the order or the size of the list though... Commented Mar 11, 2014 at 9:26

2 Answers 2

1

You should take a look at the .set(int index, E element) method. This would allow you to replace an old element with a new one, so in your case, you could have something like so: total_compare.set(j, total_compare.get(j) + " == neg == " + negative_words.get(k) +"\n");

Sign up to request clarification or add additional context in comments.

Comments

0
    public static List<String> pos_compare(List<String> pos_words, List<String>sentiment,List<String>positive_tweets , List<String>negative_words )
    {

    int pos_count = 0;
    int total_tweets = 0;



            for(int j =0 ; j < sentiment.size(); j++)
            {
                total_tweets ++;

                //Search each tweet with words from pos.word
                for(int  k = 0; k < pos_words.size(); k++)
                {
                    if(sentiment.get(j).contains(pos_words.get(k)))
                    {

                        pos_count ++;

                    }

                }
                //Search for negative
                for(int i  = 0 ; i< negative_words.size() ; i++)
                {
                    if(sentiment.get(j).contains(negative_words.get(i)))
                    {

                        pos_count --;

                    }
                }
                if ((pos_count) > 0)
                {
                    positive_tweets.add(sentiment.get(j) + "positive" + "\n" );
                }
                else if((pos_count) == 0)
                {
                    positive_tweets.add( sentiment.get(j) +  "neutral" +"\n");
                }
                else if((pos_count) < 0)
                {
                    positive_tweets.add( sentiment.get(j) + "negative" + "\n");
                }


                pos_count = 0;
            }
            //System.out.print(positive_tweets + "\n");
            return positive_tweets;
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.