title: Java function to get input from scanner until int?
body: <p>I'm attempting to make a java function that returns an <code>int</code> inputted by the user, but won't return until the user inputs a valid number. Here's my initial model of the function:</p>
  
  <pre><code>    public int getChoice(){
          try{
              return scan.nextInt();
          }catch(Exception e){
              return getChoice();
          }
      }
  </code></pre>
  
  <p><code>scan</code> is declared by <code>Scanner scan = new Scanner(System.in);</code></p>
  
  <p>This function resulted in a <code>Java.lang.stackOverflowError</code>(hmm... this seems an appropriate website for that...). I figure this is because the function is being constantly called.</p>
  
  <p>I've considered somehow using <code>Integer.valueOf(scan.nextLine())'</code>, but the reason I haven't really done anything with it is because at some points, and I can't figure out what determines whether this happens or not, but pressing 'Enter' when the program is calling <code>nextLine()</code> will skip the next <code>nextLine()</code>. I can't really figure out a way around that.</p>
  
  <p>So if anyone could possibly provide me with a Java function that will loop until the user inputs a valid integer, then return that integer, please do so, thank you.</p>