alright says now time for another
challenge this is the minimum element
challenge so to start with write a
method called read integers that's got a
parameter called count that represents
how many integers the user needs to
enter so the method needs to read from
the console until all the numbers have
been entered and then return an array
containing those numbers that were
entered secondly write a method find min
that uses the array as a parameter so
the method needs to return the minimum
value in that array so in the main
method then read the count from the
console and call the method read
integers with the count as parameter the
account variable as the parameter and
then call the find min method passing
the array returned from the call to the
read integers method and finally print
the minimum element in the array which
should be returned from the find min
method and a few tips here assume that
the user will only enter numbers so in
other words you don't need to do any
validation for your console important
for simplicity I recommend you create a
scanner as a static private field to
help with that are important and thirdly
use the project name min element
challenge when creating a new console
project in IntelliJ so that's it pause
the video now and try out your challenge
and when you're ready to come back and
see my solution you can restart the
video at that point so pause the video
now and I'll see you when you get back
all right so welcome back I hope you I
managed to do well with that challenge
let's go through my solution to it so
I'm going to start by creating this
static field that I mentioned that T
should do by typing private static
scanner scanner and it's equal to new
scanner it's going to be system dot end
that's what I've usually defined it and
in terms of the input there I'm going to
make sure that I've selected Java dot
util and again that's what we've done
each time now by setting the scan it was
a static field as I've done there what
we needed to do that so that the other
static methods like the existing main
method and the read integers method that
we're going to create they'll then be
able to access this field directly in
other words the static methods can use
scanner without creating separate
instances of that class now since we've
only got this one main class the methods
in main can also be static and should be
and you can see by default the main
method has got that static keyword there
on line 9 now additionally offset
scanner to private so that other classes
can't access it even though this doesn't
necessarily matter for this program
since we don't have any other classes
it's a good idea to keep all fields
private to encapsulate the data all
right so let's now break this down into
smaller pieces first we need to write
the method read integers that's got the
integer count or the integer with the
parameter name count so let's create
that I'm going to put that just below
the main method so then use private here
as well so private static and we're
going to return an int array of int and
left or right square brackets read
integers is the name of the method
parentheses int count the left to right
curly braces to create the method now
this method needs to return an int array
and it contains the parameter count as
we've defined on line 13 now the
parameter represents how many numbers
we're going to read from the console so
let's now implement the method firstly
we have to create and enter a and then
populate the array with the users input
which will be retrieved obviously
the console so to create this array
first I'm going to type int left or
right square brackets array equals new
int in left square brackets count now as
we've loaded previously this code
creates an array and initializes all the
elements to 0 so that every slot or
element if you will in the array
contains the value of 0 so moving
forward we're going to assume that the
user will always enter numbers only so
in other words we don't need to validate
the input at all so with that in mind
we're going to use the next int method
so we need a for loop know to read the
numbers so for the for loop we're going
to type 4 in parentheses int I equals 0
I less than a ray dot length semicolon I
plus plus then I'm going to type a
system output line we're going to put
enter a number the colon on the end of
that just so the app the screen is
actually showing what the user should do
and we're going to type into number
equals scanner dot next int you've seen
that before we just retrieving an
integer from the console and storing in
this case into a variable called
number which is of type int of course
then we need to call at the next line
method from scanner so scanner dot next
line and we're doing it to make sure we
handle the enter key being pressed once
we've entered the number and they can
then continue to enter more numbers so
we're avoiding the problem that will
happen if we don't do that we don't need
to record the number that's been entered
in other words populated into the array
we do that by typing array left or right
square brackets I'm gonna put I in that
left or right square brackets equals and
the value entered on the console which
was number and then finally what we want
to do is just return the entire array so
we do that by just typing at the turn
array right so that completes the method
read integers our next step in it is to
call this method from the main method
but before that we need to read the
count from the console using the next
int method in other words we need to
determine
how many elements are to be entered so
to do that I'm going to come back to our
main method I'm going to start with a
message in top inter count then I'm
going to top int count is equal to
scanner dot next int so retrieving how
many elements and then we're going to do
scanner dot next line so again process
the enter key and now we need to make a
call to the read integers method and we
need to save the value that was returned
the array in this case tool variable so
I'm going to type int left in white
square brackets I'm gonna call this one
returned array equals read integers and
we're gonna pass count which was the
number entered from the keyboard or on
the keyboard the basic we're signing the
return value which is the array from the
read integers method and saving it into
a new local variable called return array
which is itself an integer array so next
step now is been only to write the find
min method which will find the minimum
number in the array so let's go and
below the last curly brace closing off
the read integers method and create a
new method in there or blow that I
should say and we're going to start by
typing private static int find min in
parentheses we're going to have an array
some arguments as in to left and right
square brackets all right and I'm going
to start by setting a local variable
called min to the highest number that an
integer can hold so I'm going to type
int min is equal to integer dot I'm
going to select Max and score value now
we use that previously in the minimum
and maximum challenge earlier in the
course I refer back to that if you're
unsure what the max Allen score value is
all about but a question you might be
asking at this point is why am I
actually setting the min variable here
to the maximum value that and an integer
can actually hold well every number the
user enters must be equal or less than
the maximum value an integer can contain
so if we were to set the this min
variable to zero and the user would
enter for example all
positive numbers then the method will
return zero as the minimum since all
positive numbers were greater than zero
even if the user never actually entered
zero so by starting with the maximum
integer value what we're doing is
ensuring that the minimum value returned
was actually input received from the
user
now another approach would be to set min
to the first element of the array and
then compare it with each of the others
but you can see an example of that if
you go back and check out again the
minimum and maximum challenge earlier in
the course because I discussed that in
detail and in fact shows some of that as
a potential solution the point here is
that there's many different ways to code
a particular challenge solution but in
any event we've gone here with the max
underscore value version for an integer
all right so the next thing we now need
to do is we need to cycle through the
array and find the smallest number the
minimum value so we're going to create
another for loop that does the same as
what we did with the for loop in the
reading digis method so for int I equals
0 I less than array darlinks then I plus
plus obviously separating both with
semicolons and open the code block with
a left-to-right curly braces so to start
with we need to retrieve the value of
the element at position I and we do that
by typing int value equals array and
left or right square brackets I so we've
got the element from the array to
compare and now with each loop iteration
we want to compare the element we've
just retrieved with the current value of
min so the test if value is less than
men we can use an if statement if
parentheses value is less than min we'll
open another code block then I just type
min equals value
so if value is less than min meaning our
condition is true then we've found a
number lower than the current minimum so
we then assign its value to the min
variable and finally after the for loop
ends we can add a return statement
return min which will then be the
smallest or minimum number in the array
okay so that's our find min method so we
don't need to place a call to that
inside our main method so let's go back
up to the main method and we're going to
put that on the line after our call to
the read integers method we're going to
save the valley return int returned min
is equal to find min and we're going to
pass us an argument returned array to
that find and min method so we're
passing the array that the users entered
from the read integers method to the
find min method some of the minimum
value can be calculated and then finally
let's print out the minimum values and
min equals double quotes plus returned
min so that should complete the
challenge but let's actually run the
application to confirm that it works all
right so first this asking is to enter
count so I'm going to enter the number 8
8 elements there were nearly 10 nearly
to enter a number so I'm going to enter
some numbers 11 55 7 4 3 99 22 9 we can
see down the bottom there it's got min
equals 3 and since the number 3 is the
lowest of all the numbers we entered we
know that our application is working
correctly all right so that was my
solution to the challenge I hope you
enjoyed it and managed to learn a few
new things let's move on now to the next
video