• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

duplicate item

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79464
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
  •  
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic