Problematic exception

Hi guys

I'm attempting to write a piece of software that analyses sentences for certain emotionally relevant words. This works fine until I attempt to analyse words I have stored for positive relevance, an action which generates an array out of bounds exception. Maybe it's just because it's late, but I cannot for the life of me see where the problem is.
I'll post the whole code below, as I would also appreciate any general coding tips you guys can provide (this is, after all, a very slap dash solution). Thanks for your time people.

public static void main(String[] args)
{

//Various single strings for use in program

String name = new String();
String conversation = new String();
String userName = new String();
String thisEmotion = new String();
String holdingString = new String();

//String arrays for holding program variables

String[] context = {};
String[] conversationCues = {"furious", "thanks", "tense","kill", "love", "good", "lousy",
"ashamed", "worried", "scared", "damn", "impressed", "hate", "envy", "afraid"};

String[] Meaning = {"anger", "gratitude", "anger", "anger", "admiration", "satisfaction",
"dissapointment", "shame", "concern", "fright", "disatisfaction", "admiration", "anger",
"jealousy", "fright"};

String[] Positives = {"gratitude", "admiration", "satisfaction"};
String[] Negatives = {"anger", "dissapointment", "shame", "concern", "fright",
"disatisfaction", "jealousy"};

String[] participants = new String[20];
String[] Directives1stPerson = {"Me", "I", "Myself"};
String[] Directives3rdPerson = {"you", "him", "her", "them", "those", "he", "she"};

String[] personOneConv = {
"I am furious with you Mark",
"But I have to say thanks",
"You are awful",
"I have a headache",
"I don't get why you wont work",
"Damn"
};
String[] personTwoConv = {"I thought you would love it","I got you a present"};
String testString = new String();
String append = new String();

String holdWord = new String();
String holdEmotion = new String();


//Various ints for use in program

int arrayLength = personOneConv.length;
int negativity = 0;
int positivity = 0;
int pos = 0;

//Archiving arrays for words found

String[] p1Archive = new String[arrayLength];
String[] p2Archive = {};

String[] words = {};

//Scanner object for user input

Scanner scannerObject = new Scanner (System.in);

//User name input

System.out.println("Enter name of participant. Enter END to continue");
userName = scannerObject.next();

while (!name.equals("END"))
{
System.out.println("Enter name of participant. Enter END to continue");
name = scannerObject.next();
participants[pos] = name;
pos = pos++;
}


//For each line of person 1s conversation

for (int i = 0; i < personOneConv.length; i++)
{

//Split the string into its seperate elements
holdingString = personOneConv[i];
words = holdingString.split(" ");

//Re-initialise the emotion
thisEmotion = "";

//For each word in the string
for (int i2 = 0; i2 < words.length; i2++)
{
for (int i3 = 0; i3 < conversationCues.length; i3++)
{

testString = words[i2];
//Test for presence in emotional word bank
if (words[i2].equals(conversationCues[i3]))
{
//Archive the emotion
thisEmotion = Meaning[i3];
p1Archive[i] = thisEmotion;

}
}
}

//Tests for direction of the emotion
for (int myI = 0; myI < participants.length; myI++)
{
if(testString.equals(participants[myI]))
{
System.out.println(participants[myI]);
append=(" towards " + participants[myI]);
}
}

//Output a description of emotion felt during this sentence
System.out.println(userName + " feels " + thisEmotion + append);

}

//Reproduce archive
System.out.println("During this conversation, " + userName + " felt");

for(int i4 = 0; i4 < 100; i4++)
{
holdWord = p1Archive[i4];
System.out.println(holdWord);

//Check archived word for positivity
for (int i5 = 0; i5 < 100; i5++)
{
holdWord = p1Archive[i4];
holdEmotion = Positives[i5];

if(holdEmotion.equals(holdWord))
{
positivity = positivity + 1;
System.out.println("Emotion has shifted towards the positive");
}

holdEmotion = Negatives[i5];

//Check archived word for negativity

if(holdEmotion.equals(holdWord))
{
negativity = negativity + 1;
System.out.println("Emotion has shifted towards the negative");
}

}
}
//Notify of emotion scores for this conversation

System.out.println("The positivity score for this conversation was " + positivity);
System.out.println("The negativity score for this conversation was " + negativity);

//Check status of positive and negative scores
//Give overall status

if (positivity < negativity)
{
System.out.println("The speaker felt mostly negativity during this conversation");
} else
if (negativity < positivity)
{
System.out.println("The speaker felt mostly negativity during this conversation");
}
else
{
System.out.println("The speaker was tranquil during this conversation");
}
}
}

Comments

  • Hey,
    In several places the program limits looping based on the size of the data it is working with which prevents an out of bounds exception.
    The program fails because that principle is not followed, it fails in statment
    [code]holdEmotion = Positives[i5];[/code] because i5 is used to index
    a Positives element that does not exist.

    Check for this problem in all the program [color=Blue]for[/color] loops.

    regards, se52
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion