• 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

Using Lists/ArrayLists, code doesn't work

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I'm trying to pass a Model (the name of a class) into a method but I'm not getting any results, here is the code;



The output I get is nothing, when it should just generate 10 number 1's. As the player1 and player 2 WS variable values are different. I've tracked the problem down to the "If player1.WS > player2.WS" line in the code, I'm not sure why it doesn't make a comparrison between the two different instances of the Model class.

Can anyone please suggest why my code isn't working as I expected?

Thanks
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Based on that code, I wouldn't expect any output. You just add the models to the list - you don't do anything with them. Is there something missing?
 
David Pountney
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh dear, Just made an epic fail.

I didn't call the method...

Thanks for pointing that out to me, I'll get back to you if the problem I mentioned keeps happening.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I think that you should use to compare object the Comparable Interface.

Look here:
Inteface
 
David Pountney
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I called the method that outputs (using System.out.println) text on the screen but still I don't get anything, any ideas why?

Here is is the slightly updated code;




Thanks again
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're passing the same Model object to closeCombatHitting for both parameters. Which means that player1 == player2, so neither test will work.

I don't quite know what you're intending to happen, but maybe you need something like this?
 
David Pountney
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for pointing that out, I've corrected it and my program does what I wanted it to do.

Just so I know, would this be a good way to remove the last element in a list?

 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic