title: Identifying the second lowest and second highest value in Java
body: <p>This is not only working when you input two or more same value, it will make the output all zero instead of giving the same result as first highest and second highest. Also we can't use array and arraysort or any sorting function.</p>
  
  <p>For example if I input 4 4 2 2 1, it will make all the result 0 instead of giving:</p>
  
  <pre><code>first highest: 4
  second highest:4
  second lowest :2
  first lowest :1
  </code></pre>
  
  <p>Here's my code:</p>
  
  <pre><code>import java.util.Scanner;
  
  public class Pres {
  
  public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      System.out.print("Enter 5 numbers: ");
      int fh = 0, sh = 0, sl = 0, fl = 0;
      int a = s.nextInt();
      int b = s.nextInt();
      int c = s.nextInt();
      int d = s.nextInt();
      int e = s.nextInt();
      if (a &lt; b &amp;&amp; a &lt; c &amp;&amp; a &lt; d &amp;&amp; a &lt; e) {
          fl = a;
      }  if (b &lt; a &amp;&amp; b &lt; c &amp;&amp; b &lt; d &amp;&amp; b &lt; e) {
          fl = b;
      }  if (c &lt; a &amp;&amp; c &lt; b &amp;&amp; c &lt; d &amp;&amp; c &lt; e) {
          fl = c;
      }  if (d &lt; a &amp;&amp; d &lt; b &amp;&amp; d &lt; c &amp;&amp; d &lt; e) {
          fl = d;
      }  if (e &lt; a &amp;&amp; e &lt; b &amp;&amp; e &lt; c &amp;&amp; e &lt; d) {
          fl = e;
      }
      if (a &gt; b &amp;&amp; a &gt; c &amp;&amp; a &gt; d &amp;&amp; a &gt; e) {
          fh = a;
      }  if (b &gt; a &amp;&amp; b &gt; c &amp;&amp; b &gt; d &amp;&amp; b &gt; e) {
          fh = b;
      }  if (c &gt; a &amp;&amp; c &gt; b &amp;&amp; c &gt; d &amp;&amp; c &gt; e) {
          fh = c;
      }  if (d &gt; a &amp;&amp; d &gt; b &amp;&amp; d &gt; c &amp;&amp; d &gt; e) {
          fh = d;
      }  if (e &gt; a &amp;&amp; e &gt; b &amp;&amp; e &gt; c &amp;&amp; e &gt; d) {
          fh = e;
      }
      //sh
      if (a &gt; b &amp;&amp; a &gt; c &amp;&amp; a &gt; d &amp;&amp; a &lt; e) {
          sh = a;
      }  if (a &gt; b &amp;&amp; a &gt; c &amp;&amp; a &gt; d &amp;&amp; a &lt; e) {
          sh = a;
      }  if (a &lt; b &amp;&amp; a &gt; c &amp;&amp; a &gt; d &amp;&amp; a &gt; e) {
          sh = a;
      }  if (a &gt; b &amp;&amp; a &lt; c &amp;&amp; a &gt; d &amp;&amp; a &gt; e) {
          sh = a;
      }  if (a &gt; b &amp;&amp; a &gt; c &amp;&amp; a &lt; d &amp;&amp; a &gt; e) {
          sh = a;
      }
      if (b &gt; a &amp;&amp; b &gt; c &amp;&amp; b &gt; d &amp;&amp; b &lt; e) {
          sh = a;
      }  if (b &gt; a &amp;&amp; b &gt; c &amp;&amp; b &gt; d &amp;&amp; b &lt; e) {
          sh = b;
      }  if (b &lt; a &amp;&amp; b &gt; c &amp;&amp; b &gt; d &amp;&amp; b &gt; e) {
          sh = b;
      }  if (b &gt; a &amp;&amp; b &lt; c &amp;&amp; b &gt; d &amp;&amp; b &gt; e) {
          sh = b;
      }  if (b &gt; a &amp;&amp; b &gt; c &amp;&amp; b &lt; d &amp;&amp; b &gt; e) {
          sh = b;
      }
      if (c &gt; b &amp;&amp; c &gt; a &amp;&amp; c &gt; d &amp;&amp; c &lt; e) {
          sh = c;
      }  if (c &gt; b &amp;&amp; c &gt; a &amp;&amp; c &gt; d &amp;&amp; c &lt; e) {
          sh = c;
      }  if (c &lt; b &amp;&amp; c &gt; a &amp;&amp; c &gt; d &amp;&amp; c &gt; e) {
          sh = c;
      }  if (c &gt; b &amp;&amp; c &lt; a &amp;&amp; c &gt; d &amp;&amp; c &gt; e) {
          sh = c;
      }  if (c &gt; b &amp;&amp; c &gt; a &amp;&amp; c &lt; d &amp;&amp; c &gt; e) {
          sh = c;
      }
      if (d &gt; b &amp;&amp; d &gt; c &amp;&amp; d &gt; a &amp;&amp; d &lt; e) {
          sh = d;
      }  if (d &gt; b &amp;&amp; d &gt; c &amp;&amp; d &gt; a &amp;&amp; d &lt; e) {
          sh = d;
      }  if (d &lt; b &amp;&amp; d &gt; c &amp;&amp; d &gt; a &amp;&amp; d &gt; e) {
          sh = d;
      }  if (d &gt; b &amp;&amp; d &lt; c &amp;&amp; d &gt; a &amp;&amp; d &gt; e) {
          sh = d;
      }  if (d &gt; b &amp;&amp; d &gt; c &amp;&amp; d &lt; a &amp;&amp; d &gt; e) {
          sh = d;
      }
      if (e &gt; b &amp;&amp; e &gt; c &amp;&amp; e &gt; d &amp;&amp; e &lt; a) {
          sh = e;
      }  if (e &gt; b &amp;&amp; e &gt; c &amp;&amp; e &gt; d &amp;&amp; e &lt; a) {
          sh = e;
      }  if (e &lt; b &amp;&amp; e &gt; c &amp;&amp; e &gt; d &amp;&amp; e &gt; a) {
          sh = e;
      }  if (e &gt; b &amp;&amp; e &lt; c &amp;&amp; e &gt; d &amp;&amp; e &gt; a) {
          sh = e;
      }  if (e &gt; b &amp;&amp; e &gt; c &amp;&amp; e &lt; d &amp;&amp; e &gt; a) {
          sh = e;
      }
      //sl
      if (a &lt; b &amp;&amp; a &lt; c &amp;&amp; a &lt; d &amp;&amp; a &gt; e) {
          sl = a;
      }  if (a &gt; b &amp;&amp; a &gt; c &amp;&amp; a &gt; d &amp;&amp; a &lt; e) {
          sl = a;
      }  if (a &lt; b &amp;&amp; a &gt; c &amp;&amp; a &gt; d &amp;&amp; a &gt; e) {
          sl = a;
      }  if (a &gt; b &amp;&amp; a &lt; c &amp;&amp; a &gt; d &amp;&amp; a &gt; e) {
          sl = a;
      }  if (a &gt; b &amp;&amp; a &gt; c &amp;&amp; a &lt; d &amp;&amp; a &gt; e) {
          sl = a;
      }
      if (b &lt; a &amp;&amp; b &lt; c &amp;&amp; b &lt; d &amp;&amp; b &gt; e) {
          sl = a;
      }  if (b &lt; a &amp;&amp; b &lt; c &amp;&amp; b &lt; d &amp;&amp; b &gt; e) {
          sl = b;
      }  if (b &gt; a &amp;&amp; b &lt; c &amp;&amp; b &lt; d &amp;&amp; b &lt; e) {
          sl = b;
      }  if (b &lt; a &amp;&amp; b &gt; c &amp;&amp; b &lt; d &amp;&amp; b &lt; e) {
          sl = b;
      }  if (b &lt; a &amp;&amp; b &lt; c &amp;&amp; b &gt; d &amp;&amp; b &lt; e) {
          sl = b;
      }
      if (c &lt; b &amp;&amp; c &lt; a &amp;&amp; c &lt; d &amp;&amp; c &gt; e) {
          sl = c;
      }  if (c &lt; b &amp;&amp; c &lt; a &amp;&amp; c &lt; d &amp;&amp; c &gt; e) {
          sl = c;
      }  if (c &gt; b &amp;&amp; c &lt; a &amp;&amp; c &lt; d &amp;&amp; c &lt; e) {
          sl = c;
      }  if (c &lt; b &amp;&amp; c &gt; a &amp;&amp; c &lt; d &amp;&amp; c &lt; e) {
          sl = c;
      }  if (c &lt; b &amp;&amp; c &lt; a &amp;&amp; c &gt; d &amp;&amp; c &lt; e) {
          sl = c;
      }
      if (d &lt; b &amp;&amp; d &lt; c &amp;&amp; d &lt; a &amp;&amp; d &gt; e) {
          sl = d;
      }  if (d &lt; b &amp;&amp; d &lt; c &amp;&amp; d &lt; a &amp;&amp; d &gt; e) {
          sl = d;
      }  if (d &gt; b &amp;&amp; d &lt; c &amp;&amp; d &lt; a &amp;&amp; d &lt; e) {
          sl = d;
      }  if (d &lt; b &amp;&amp; d &gt; c &amp;&amp; d &lt; a &amp;&amp; d &lt; e) {
          sl = d;
      }  if (d &lt; b &amp;&amp; d &lt; c &amp;&amp; d &gt; a &amp;&amp; d &lt; e) {
          sh = d;
      }
      if (e &lt; b &amp;&amp; e &lt; c &amp;&amp; e &lt; d &amp;&amp; e &gt; a) {
          sl = e;
      }  if (e &lt; b &amp;&amp; e &lt; c &amp;&amp; e &lt; d &amp;&amp; e &gt; a) {
          sl = e;
      }  if (e &gt; b &amp;&amp; e &lt; c &amp;&amp; e &lt; d &amp;&amp; e &lt; a) {
          sl = e;
      }  if (e &lt; b &amp;&amp; e &gt; c &amp;&amp; e &lt; d &amp;&amp; e &lt; a) {
          sl = e;
      }  if (e &lt; b &amp;&amp; e &lt; c &amp;&amp; e &gt; d &amp;&amp; e &lt; a) {
          sl = e;
      }
      if (a &lt; fl) {
          sh = fl;
          fl = a;
      }
      if ((a &gt; fl) &amp;&amp; (a &lt; sl)) {
          sl = a;
      }
      if (b &lt; fl) {
          sl = fl;
          fl = b;
      }
      if ((b &gt; fl) &amp;&amp; (b &lt; sl)) {
          sl = b;
      }
      if (c &lt; fl) {
          sh = fl;
          fl = c;
      }
      if ((c &gt; fl) &amp;&amp; (c &lt; sl)) {
          sh = c;
      }
      if (d &lt; fl) {
          sl = fl;
          fl = d;
      }
      if ((d &gt; fl) &amp;&amp; (d &lt; sl)) {
          sl = d;
      }
      if (e &lt; fl) {
          sl = fl;
          fl = e;
      }
      if ((e &gt; fl) &amp;&amp; (e &lt; sl)) {
          sl = e;
      }
      else
      fh = sh;
      fl = sh;
      sl = sh;
      sh = fl;
  
  
      System.out.println("First highest  :" + fh);
      System.out.println("Second highest :" + sh);
      System.out.println("First lowest   :" + fl);
      System.out.println("Second lowest  :" + sl);
  </code></pre>