title: Return first and last digits of an integer using while loop
body: <p>I've been trying to write a program that uses a while loop to return the first and last digits entered. </p>
  
  <p>Here's what I've got so far:</p>
  
  <pre><code>import java.util.Scanner;
  public class FirstLastInt {
  public static void main(String[]args)
  {
      int fdig, ldig, num;
  
      Scanner in = new Scanner(System.in);
  
      System.out.println("Enter an Integer.");
      num = in.nextInt();
  
      ldig = num%10;
  
      while (num &gt; 0)
      { fdig = num/10;
          num = num/10;
  
           System.out.println("The first digit is " + fdig + ", and the last digit is " + ldig + ".");
      }
  }
  </code></pre>
  
  <p>num is the integer entered, fdig is to be the first digit, and ldig is to be the last digit.</p>
  
  <p>I figured the last digit will always be num%10, and I realized so far using the while loop like it's written will return the first digit......Along with a bunch of other numbers I don't really need. </p>
  
  <p>For example: </p>
  
  <pre><code>run:
  Enter an Integer.
  98884
  The first digit is 9888, and the last digit is 4.
  The first digit is 988, and the last digit is 4.
  The first digit is 98, and the last digit is 4.
  The first digit is 9, and the last digit is 4.
  The first digit is 0, and the last digit is 4.
  BUILD SUCCESSFUL (total time: 3 seconds)
  </code></pre>
  
  <p>It looks like the loop runs before and after I get the digit I want. Is there anyway to specify which line I'd like printed? (In the example, it's "The first digit is 9, and the last digit is 4.")</p>