Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Problematic exception Posted by robWhittle on 29 Mar 2010 at 2:42 PM
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");
}
}
}

Report
Re: Problematic exception Posted by silveredge52 on 1 Apr 2010 at 5:37 AM
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
holdEmotion = Positives[i5];
because i5 is used to index
a Positives element that does not exist.

Check for this problem in all the program for loops.

regards, se52



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.