title: How to get the minimum and maximum value inside a user-defined array?
body: <p>I want to get the minimum value and maximum value inside an array. After the user inputs n number of arrays, it will print the minimum and max value inside that array.</p>
  <p>Here is an example output</p>
  <p><a href="https://i.stack.imgur.com/gG2Yc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gG2Yc.png" alt="enter image description here" /></a></p>
  <p>Here is my code so far</p>
  <pre><code>public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
          try {
              System.out.println(&quot;Enter array size: &quot;);
              int n = input.nextInt();
  
              int [] array = new int[n];
              int max = getMaxValue(array);
              int min = getMinValue(array);
  
              System.out.println(&quot;Enter &quot; + n + &quot; elements:&quot;);
              for (int i = 1; i &lt;= array.length; i++) {
                  array[i] = input.nextInt();
              }
  
              System.out.println(&quot;Max Value: &quot; + max);
              System.out.println(&quot;Min Value: &quot; + min);
  
          } catch (InputMismatchException e) {
              System.out.println(&quot;INVALID INPUT &gt;&gt; PLEASE INPUT A NUMBER&quot;);
          }
      }
  
      private static int getMaxValue(int[] array) {
          int maxValue = array[0];
          for (int i = 1; i &lt; array.length; i++){
              if (array[i] &gt; maxValue){
                  maxValue = array[i];
              }
          }
          return maxValue;
      }
  
      private static int getMinValue(int[] array) {
          int minValue = array[0];
          for (int i = 1; i &lt; array.length; i++){
              if (array[i] &lt; minValue) {
                  minValue = array[i];
              }
          }
          return minValue;
      }
  }
  </code></pre>