title: Checking for user input to be only integers in Java
body: <p>I'm doing lottery game for my assignment (user inputs 6 number, i will generate 8 unique winning numbers and 2 last numbers are supplementary). I need help with user input checking if input numbers are from 1 to 45 and input must be int, when input is not integer it throws an error.</p>
  
  <p>This programming way is procedure way, how can i change it into object oriented way? I know that I must make methods in another java file and then link it back to this main. Can you suggest me how to do it?</p>
  
  <p>I have tried try and catch, if and else (for input check) but i don't know how to check user input when it's in array. Thank you for help.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>class Lottery {
  
  public static void main ( String[] args ) {
  
      System.out.println("\nWelcome to the Lottery game.");
      System.out.println("You can enter numbers from 1 to 45.");
  
      // User input into an array
      int[] input = new int[6];
  
      Scanner scanner = new Scanner(System.in);
  
      System.out.println("\nPlease enter your 6 lucky numbers: ");
      for(int j = 0; j &lt; 6; j++) {
  
          input[j]=scanner.nextInt();
      }
  
      int check = scanner.nextInt();
      if(check &lt; 0 &amp;&amp; check &gt; 45) {
          System.out.println("\nERROR: Please enter only numbers from 1 to 45!");
      }
  
      // Printing out unique winning numbers from random generator
      System.out.println("\nWinning numbers: ");
      MultiRandomGenerator mrg = new MultiRandomGenerator();
      int[] set;
  
      set = mrg.getSet();
      for (int i = 0; i &lt; set.length; i++) {
  
          System.out.print(set[i] + " ");
      }
  
      // Loops for counting how many numbers user has guessed right
      int count = 0; // for 6 numbers
      int scount = 0; // for 2 last supplementary numbers
  
      for(int i = 0; i &lt; input.length; i++) {
  
          for(int k = 0; k &lt; set.length; k++) {
  
              if (k &lt; 6) {
  
                  if (set[k] == input[i]) {
  
                      count++;
                  } else {
                      if (set[k] == input[i]) {
  
                      scount++;
                      }
  
                  }
  
              }
  
          }
      }
      System.out.print("\n\nYou guessed right " + count + " winning numbers.");
      System.out.print("\nYou guessed right " + scount + " suplementary numbers.");
  
      // If statments for printing out winning prizes
      if (count == 6) {
  
          System.out.println("\nYou have won 1st price!");
      } if (count == 5 &amp;&amp; scount == 1) {
  
          System.out.println("\nYou have won 2st price!");
      } if (count == 5) {
  
          System.out.println("\nYou have won 3st price!");
      } if (count == 4) {
  
          System.out.println("\nYou have won 4st price!");
      } if (count == 3 &amp;&amp; scount == 1) {
  
          System.out.println("\nYou have won 5st price!");
      } if (count == 1 &amp;&amp; scount == 2) {
  
          System.out.println("\nYou have won 6st price!");
      } else {
  
          System.out.println("\nSorry, you didn't won anything.");
      }
  }
  }
  </code></pre>