Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 16th, 2005, 1:23 PM   #1
RandomRetard
Newbie
 
RandomRetard's Avatar
 
Join Date: Oct 2005
Location: Behind you
Posts: 8
Rep Power: 0 RandomRetard is on a distinguished road
Send a message via MSN to RandomRetard
Question IndexOutOfBoundException

I'm doing a project which involves reading in a list of people from a file then matching them up in the most efficiant way possible. For anything that they both like, the 'dating agency' get £10 but for everything one of them likes and the other dislikes, the agency loses £5.

The file is a txt file that has the format:

Gender "m" or "f"
Surname
Forename
Likes (stored as a string but each letter represents one of 6 things, e.g fot would be f = football, o = opera and t = theatre)
Dislikes (stored in the same way above)

I've read from the file and put the men in one ArrayList and the women in another ArrayList

An attempt to optomise profit is all thats needed (as there will be an unknown number of customers and so covering all possibilities would be virtually impossible) so my idea was to look at each man and find his optimum woman. Obviously, this may not be optimum so I will repeat it a few times starting with a different man or woman but only after I sort this code...

I get an IndexOutOfBoundsException at line 126 of the code show below. I understand that this means I'm looking for a position in the ArrayList that doesn't exist yet but I'm sure it does. This tells me I've made a logical error somewhere but I have no idea where

Also, I know this code currently allows polygamy but I haven't completely finished yet

This code is the method thats giving me hell...
78  public void matchMaker(){
79	  ArrayList bestMatch = new ArrayList();
80
... 	Iterator i = manArray.iterator();
		int count = 0;
		int countW = 0;
		int highestSoFar = 0;
		int position = 0;
		while (i.hasNext()){
			int profit = 0;
			Customer thisCustomer = (Customer)i.next();
			for (int likePos = 0; likePos < thisCustomer.getLikes().length(); likePos++){
			 char heLikes = thisCustomer.getLikes().charAt(likePos);
				Iterator j = womanArray.iterator();
				countW = 0;
				while (j.hasNext()){
					profit = 0;
		 		Customer thatCustomer = (Customer)j.next();
		 		for (int likePosW = 0; likePosW < thatCustomer.getLikes().length(); likePosW++){
		 			char sheLikes = thatCustomer.getLikes().charAt(likePosW);
		 			if (heLikes == sheLikes){
		 		 	profit = profit+10;
						}
					}

		 		for (int dislikePosW = 0; dislikePosW < thatCustomer.getDislikes().length(); dislikePosW++){
		 			char sheDislikes = thatCustomer.getDislikes().charAt(dislikePosW);
		 			if (heLikes == sheDislikes){
		 		 	profit = profit-5;
						}
					}
					if (profit > highestSoFar){
		 			highestSoFar = profit;
		 			position = countW;
					}
					countW++;
				}
			}

			Customer thatCustomer = (Customer)womanArray.get(position);
			if (bestMatch.isEmpty()){
				Matched isMatched = new Matched
		 	(thisCustomer.getGender(), thisCustomer.getSurname(), thisCustomer.getForename(), thisCustomer.getLikes(), thisCustomer.getDislikes(),
		 	 thatCustomer.getGender(), thatCustomer.getSurname(), thatCustomer.getForename(), thatCustomer.getLikes(), thatCustomer.getDislikes(), profit);
				bestMatch.add(isMatched);
			}

			else{
126 			Matched thisMatch = (Matched)bestMatch.get(count);
				int arrayProfit = thisMatch.getProfit();
				if (profit > arrayProfit){
					Matched isMatched = new Matched
		 		(thisCustomer.getGender(), thisCustomer.getSurname(), thisCustomer.getForename(), thisCustomer.getLikes(), thisCustomer.getDislikes(),
		 		 thatCustomer.getGender(), thatCustomer.getSurname(), thatCustomer.getForename(), thatCustomer.getLikes(), thatCustomer.getDislikes(), profit);
					bestMatch.add(count, isMatched);
					bestMatch.remove(count);
				}
...		 }
136		 count++;
137 	}
138  }

I'm not gonna post ALL my code but if anyone wants it, I'll post it later. Sorry this is such a long post. Phew!
RandomRetard is offline   Reply With Quote
Old Dec 16th, 2005, 1:30 PM   #2
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
I don't see a condition to limit "count" to bestMatch's length so it can easily go out of bound. Try some debugging, print "count" for every "while" iteration and see what number you're erroring at.
OpenLoop is offline   Reply With Quote
Old Dec 16th, 2005, 5:09 PM   #3
B3TA_SCR1PT3R
Hobbyist Programmer
 
B3TA_SCR1PT3R's Avatar
 
Join Date: Jul 2005
Location: Dallas, Texas
Posts: 101
Rep Power: 0 B3TA_SCR1PT3R is an unknown quantity at this point
Send a message via AIM to B3TA_SCR1PT3R
Make the orignal array size bigger, maybe?
__________________
Hoes telling me to calm down but I'm like fuck that shit!
B3TA_SCR1PT3R is offline   Reply With Quote
Old Dec 16th, 2005, 6:33 PM   #4
RandomRetard
Newbie
 
RandomRetard's Avatar
 
Join Date: Oct 2005
Location: Behind you
Posts: 8
Rep Power: 0 RandomRetard is on a distinguished road
Send a message via MSN to RandomRetard
Thanks so much for your promt replies!

I did have a look at what you mentioned regarding the 'count' and spotted a different potential problem. I ended up re-writing the method
The array size shouldn't have affected it because I was using an ArrayList which told me the array wasn't being filled and the 'count' variable was going too high and therefore throwing the runtime error.

I didn't limit the count, as you suggested because the idea was that the loop can only go so far and count would then be reset when it checks a new man against all the women. It now compiles fine (like before) but it now runs without any errors!!

That is, until I tested with my 'printMatched' method... (I think its fairly obvious what that method does :p Our lecturer always nags about 'meaningful names' (one thing I'm generally not very good at!))

Anyway, I'm still working on it but now its bed time :p I just wanted to thank you for your promt response earlier and I'm sure I'll be back for more help later, heh heh.

Last edited by RandomRetard; Dec 16th, 2005 at 6:50 PM.
RandomRetard is offline   Reply With Quote
Old Dec 29th, 2005, 8:15 AM   #5
RandomRetard
Newbie
 
RandomRetard's Avatar
 
Join Date: Oct 2005
Location: Behind you
Posts: 8
Rep Power: 0 RandomRetard is on a distinguished road
Send a message via MSN to RandomRetard
So I'm back from my Xmas break and it's straight back to coding! The only problem... I don't know where to begin to fix my errors

As much as I dislike BlueJ, I've been trying to use its debugger because its generally quite good. Unfortunatly, this doesn't seem to be working either! None of my variables are being recognised in the debugger. For example, as the code goes past 'int count = 0;' nothing appears in the debugger. It doesn't even come up as null! It's like the line doesn't exist and this continues throughout the code. So I can't see whats happening and therefore I can't see whats going wrong in my code. So now it's time to turn to my other debugger... The internet:p

Here is the re-written code. As far as I can tell, the problem is that nothing is being added to the matched couples ArrayList but I suppose the error could be anywhere.

	public void matchMaker(){
		ArrayList bestMatch = new ArrayList();

		Iterator i = manArray.iterator();
		int count = 0;
		int countW = 0;
		int highestSoFar = -100;
		int position = 0;
		int profit = 0;
		while (i.hasNext()){
			Customer thisCustomer = (Customer)i.next();
			Iterator j = womanArray.iterator();
			countW = 0;
			while (j.hasNext()){
				profit = 0;
				Customer thatCustomer = (Customer)i.next();
		 	for (int likePos = 0; likePos < thisCustomer.getLikes().length(); likePos++){
		 		char heLikes = thisCustomer.getLikes().charAt(likePos);
		 		for (int likePosW = 0; likePosW < thatCustomer.getLikes().length(); likePos++){
		 			char sheLikes = thatCustomer.getLikes().charAt(likePosW);
		 			if (heLikes == sheLikes){
		 		 	profit = profit + 10;
						}
					}
		 		for (int dislikePosW = 0; dislikePosW < thatCustomer.getDislikes().length(); dislikePosW++){
		 			char sheDislikes = thatCustomer.getDislikes().charAt(dislikePosW);
		 			if (heLikes == sheDislikes){
		 		 	profit = profit - 5;
						}
					}
				}
		 	for (int dislikePos = 0; dislikePos < thisCustomer. getDislikes().length(); dislikePos++){
		 		char heDislikes = thisCustomer.getDislikes().charAt(dislikePos);
		 		for (int likePosW = 0; likePosW < thatCustomer.getLikes().length(); likePosW++){
		 			char sheLikes = thatCustomer.getLikes().charAt(likePosW);
		 			if (heDislikes == sheLikes){
		 		 	profit = profit - 5;
						}
					}
				}
				if (profit > highestSoFar){
					highestSoFar = profit;
					position = countW;
				}
				countW++;
			}

			Customer thatCustomer = (Customer)womanArray.get(position);
			if (bestMatch.isEmpty()){
				Matched isMatched = new Matched
		 	(thisCustomer.getGender(), thisCustomer.getSurname(), thisCustomer.getForename(), thisCustomer.getLikes(), thisCustomer.getDislikes(),
		 	 thatCustomer.getGender(), thatCustomer.getSurname(), thatCustomer.getForename(), thatCustomer.getLikes(), thatCustomer.getDislikes(), profit);
				bestMatch.add(isMatched);
			}

			else{
			 Matched thisMatch = (Matched)bestMatch.get(count - 1);
				int arrayProfit = thisMatch.getProfit();
				if (profit > arrayProfit){
					Matched isMatched = new Matched
		 		(thisCustomer.getGender(), thisCustomer.getSurname(), thisCustomer.getForename(), thisCustomer.getLikes(), thisCustomer.getDislikes(),
		 		 thatCustomer.getGender(), thatCustomer.getSurname(), thatCustomer.getForename(), thatCustomer.getLikes(), thatCustomer.getDislikes(), profit);
					bestMatch.remove(count-1);
		 		bestMatch.add(count - 1, isMatched);
				}
			}
			count++;
		}
	}
	
	public void printMatched(){
		Iterator x = bestMatch.iterator();
		while (x.hasNext()){
			Matched thisMatch = (Matched)x.next();
		 System.out.println(thisMatch.getGenderM()+"	 +	 "+thisMatch.getGenderW());
		 System.out.println(thisMatch.getSurnameM()+"	 +	 "+thisMatch.getSurnameW());
		 System.out.println(thisMatch.getForenameM()+"	 +	 "+thisMatch.getForenameW());
		 System.out.println(thisMatch.getLikesM()+"	 +	 "+thisMatch.getLikesW());
		 System.out.println(thisMatch.getDislikesM()+"	 +	 "+thisMatch.getDislikesW());
			System.out.println("");
		}
	}

Thanks in advance. This has been driving me crazy!
RandomRetard is offline   Reply With Quote
Old Dec 29th, 2005, 8:36 AM   #6
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
Did you set a breakpoint on the line?
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Jan 1st, 2006, 10:53 AM   #7
RandomRetard
Newbie
 
RandomRetard's Avatar
 
Join Date: Oct 2005
Location: Behind you
Posts: 8
Rep Power: 0 RandomRetard is on a distinguished road
Send a message via MSN to RandomRetard
Thanks for your reply Dameon but I'm not quite sure what you mean. Could you clarify that a bit please?
RandomRetard is offline   Reply With Quote
Old Jan 3rd, 2006, 9:08 AM   #8
RandomRetard
Newbie
 
RandomRetard's Avatar
 
Join Date: Oct 2005
Location: Behind you
Posts: 8
Rep Power: 0 RandomRetard is on a distinguished road
Send a message via MSN to RandomRetard
OK, it seems that everything I try ends in failure

So, now I'm gonna paste a link to the java files in the hope that someone can find the error by running the full code rather than just looking at my snippits.
The three files are here, here and here

Please help!! This has been driving me insane!
RandomRetard is offline   Reply With Quote
Old Jan 3rd, 2006, 1:14 PM   #9
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
One thing to note - Hungarian matching is a reasonably fast algorithm and finds ideal matchings, although it may be a bit complicated for this situation...
Klipt is offline   Reply With Quote
Old Jan 3rd, 2006, 1:40 PM   #10
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
Quote:
Originally Posted by RandomRetard
The three files are here, here and here
It's a bit difficult to test without a data file :p

Also, I think you may still have a bit to learn about object-oriented programming. For example, why not have member functions in the Customer class to (1) read the customer's details from a file (2) write them to screen (3) compare this person to another customer and calculate the profit of matching them together? It'll make your code a lot neater and reduce some of your majorly nested loops to a few function calls.

Then for your matched class, why are you transferring across all the details? Why not just have two Customer references for the people you've matched? I think you're making some things harder on yourself than necessary.
Klipt is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:47 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC