• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is this code killing almost everything, not just duplicates?  RSS feed

 
Noland Oakley
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have a project in which it is supposed to take the following contents of a text file:



and remove the duplicates and then create a new file with the same contents minus the duplicates. Here is my code:



It seems like it should work but it doesn't. When run the final file contains this:



What is wrong with my code? Thanks!

P.S. To make the purpose of this program clearer, here is a copy of the assignment:


Remove the Duplicates



Create a new project named Duplicates in a workspace folder. Add the template files and rename the template LNFI_Duplicates.

You may work with a partner on this project, but you may not work in larger groups. If you work with a partner, simply add both names as a comment to the top of the Duplicates class. You only need to hand in one copy. There is no collaboration (other than minor help) with anyone other than your partner.

Located in the shortcut folder is a text document named �puzzles.txt�. This file contains phrases that could be used for Wheel of Fortune or some similar game. The only problem is that it contains some phrases that are duplicates. For example, the phrase �Mickey Mouse� appears on the list two or more times.

You are to write a program that reads puzzles.txt and writes a second text file named puzzles2.out that is identical to puzzles.txt except there are no duplicate puzzles. Capitalization does not matter. Therefore, �Mickey Mouse� and �mickey mouse� are duplicates.

I would suggest reading each puzzle from the input file into an ArrayList and then going through the array list one puzzle at a time and removing the duplicates. This is a fairly easy assignment using an ArrayList, but it would be fairly difficult if you use an array. There may be an easier way to do it other than using an ArrayList, but I don�t know what it would be. If you think of an easier way, you may do it that way.


JCE: broke up some long lines
[ September 06, 2007: Message edited by: Joe Ess ]
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look really hard at this line:
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For grins, follow the link from the ArrayList JavaDoc to the Collection interface and then browse the links to known subinterfaces. See if you run across a description that suggests a solution to your problem in the first sentence. Not only can you claim you followed your nose to this solution but you can remember to do it often on other problems.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!