title: Find Smallest of Two numbers using Java -
body: <p>So I have an assignment where I need to find the smallest of two numbers entered using java. Here are the instructions:</p>
  
  <p>Input a set of positive integers, ending with -1 as a sentinel. Print the smallest and second smallest of these numbers in that order. These two numbers may be equal to each other – see the second example below.</p>
  
  <p>You should check that there are at least two numbers input before the -1, so that there always will be a smallest and second smallest number. (Hint: you may want to input the first two numbers separately, before you start a loop that inputs the rest of the numbers.) If the first or second number is -1, you should call IO.reportBadInput and halt the program.</p>
  
  <p>Examples:</p>
  
  <p>java TwoSmall<br>
  32<br>
  11<br>
  19<br>
  7<br>
  -1<br>
  RESULT: 7<br>
  RESULT: 11        </p>
  
  <p>I have done the assignment but I was wondering if there is a simpler way of doing this:</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public class TwoSmall
  {
  public static void main(String[]args){
  int smallest,secondSmallest,temp = 0;
  
  
  System.out.println("Please enter a number");
  smallest = IO.readInt();
  System.out.println("Please enter a number");
  secondSmallest = IO.readInt();
  
      // Used to sort intial two numbers
      if (smallest&gt;secondSmallest){
  
          temp = smallest;
          smallest = secondSmallest;
          secondSmallest = temp;
  
      }
  
      if (smallest !=-1 &amp;&amp; secondSmallest !=-1){
          while (temp != -1){
              System.out.println("Please enter a number");
              temp = IO.readInt();
                  if (temp&lt;smallest &amp;&amp; temp != -1){
                      secondSmallest = smallest;
                      smallest = temp;
                  }else if (temp&lt;secondSmallest &amp;&amp; temp != -1){
                      secondSmallest = temp;
                  }
  
          }
          IO.outputIntAnswer(smallest);
          IO.outputIntAnswer(secondSmallest);
      }else {
          IO.reportBadInput();
      }       
  }
  }
  </code></pre>
  
  <p>I feel like I have written too much code for a simple problem.</p>