Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

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

Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
Posts: 9626
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look really hard at this line:
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic