Win a copy of Serverless Applications with Node.js this week in the NodeJS 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Why won't this print smallest value on list  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
New to java and currently experimenting with ArrayList and lambda structures
I am trying to get biggest and smallest value from my ArrayList
Currently it is "almost" working. I set my function to pick up values under 30 and it does that, but only if there is only 1 value under 30
if there is two values under 30 it picks up the one that was added to list first
How do I make it that it would pick all values under 30 from list?

Container class that does contain methods for printing list


Contains stuff and variables



more variables and stuff


Main class
and yes I used eclipse for this
That's why it does have "Package test" in there

All tips are appreciated
Thank you!
 
Ranch Hand
Posts: 99
Java Linux Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the PrintSnaller method, you could try something like:

 
Gerard Gauthier
Ranch Hand
Posts: 99
Java Linux Monad
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the max and min, I'd try something like below:

 
Rancher
Posts: 3124
110
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Beginner,

welcome to the ranch and enjoy the stay!

Your method 'printSmaller' is mercyless; it removes everything from the list that is not suitable, and prints the rest.

A more friendly way is to use a Predicate<Info> as a parameter of the method. You would then get something like:

Try it for some suitable Predicates.
 
Gerard Gauthier
Ranch Hand
Posts: 99
Java Linux Monad
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correction to my earlier post.. Didn't account for the optional max/min.

 
Samuel Jeeves
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Gerard Gauthier wrote:For the PrintSnaller method, you could try something like:


Piet Souris wrote:
hi Beginner,

welcome to the ranch and enjoy the stay!

Your method 'printSmaller' is mercyless; it removes everything from the list that is not suitable, and prints the rest.

A more friendly way is to use a Predicate<Info> as a parameter of the method. You would then get something like:



Try it for some suitable Predicates.



Managed to get it to work with both methods
Thank you!
Now what if I would want to change that "30" to pick up user input instead of it being locked inside code
how should I go forward with it?
I tried few methods but I got some strange errors
 
Gerard Gauthier
Ranch Hand
Posts: 99
Java Linux Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post what you tried?
 
Sheriff
Posts: 5813
150
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To not use the hardcoded 30, just use a final or effectively final variable.
I think that will work.
 
Samuel Jeeves
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Gerard Gauthier wrote:Could you post what you tried?



To not use the hardcoded 30, just use a final or effectively final variable.

I think that will work.



This is exactly what I have been testing
but it keeps throwing me "The value of the local variable X is not used" error in main class and I can't seem to pinpoint reason

also what comes to Knute's code it would most likely work like that
BUT this is related to university project and for us scanners / communicating with user and .stream methods aren't allowed to be in same class
They requested us to keep scanners in main class and variables and "functions" in separate ones


Got it to work!
I was being stupid again and didn't realize something
Thanks for help!
 
Knute Snortum
Sheriff
Posts: 5813
150
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What was it?  We've all done stupid things before.
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!