my dog learned polymorphism*
The moose likes Beginning Java and the fly likes duplicate item Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "duplicate item" Watch "duplicate item" New topic
Author

duplicate item

Shananne DuFrame
Ranch Hand

Joined: Nov 13, 2006
Posts: 51
Hello,
wondering if anyone could help me out. Been trying this one out for awhile. I am trying to get the following to work:

macaroni was duplicated in the list.

This is suppose to be one of my first output lines.
I already have the Exception line working.
I have tried to code the duplicated item several times. Any help in the right direction would be appreciated.

I bolded where I believe my problem is.



This is my results:
Enter the input file name: C:\\TestFile5.txt

InvalidAisleException: Aisle must be 1-5: turkey shows aisle 9

The file C:\\TestFile5.txt contains the following items:
macaroni in aisle 3
chips in aisle 2
macaroni in aisle 3
salsa in aisle 1

The list print by item is:
chips in aisle 2
macaroni in aisle 3
macaroni in aisle 3
salsa in aisle 1

The list printed by aisle is:


----jGRASP: operation complete.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38044
    
  22
I think you aren't getting the Exception where you think you are.
You have indented your code incorrectly, which leads you to believe that "if(aisle > 5)" is part of the if(item.compareTo . . .) block. It isn't. It is independent.
Check carefully what you have in the text file against "turkey."

BTW: How are you working out your elements.getItem()? A compareTo doesn't give -1 when there is a duplicate. I think you are actually using your Grocery class as a Collection and you want a contains() method in it.

Find a copy of Deitel's book and you will see that every } is followed by a trailing comment. A useful habit to get into. Most people think that a programmer's text editor is a word processor, and you enter an "if" block like this:
Stage 1Stage 2Stage 3Stage 4But you shouldn't. You should do it like this:
Stage 1Stage 2Stage 3stage 4Two benefits:
  • Less risk of compiler errors because you have unbalanced {}s.
  • A comment which reminds you which block ends where.
  •  
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: duplicate item
     
    Similar Threads
    Linked List
    Two scanner FileReader
    ? in a Linked List
    Read File
    i changed a folder name and now my program wont run