title: Java exception in thread "main"
body: <p>I made a simple program which generates a random number between 1 to 100 and asks the user to enter a number between 1 and 100. If the number is more than the random number a message is displayed saying that it is more than the random number and if it is less it displays the opposite. The user only has 10 chances to guess the correct number. Here is the code:</p>
  
  <pre><code>import java.util.Scanner;
  
  public class Program {
      public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
  
          int random_num = (int) (Math.random() * 100) + 1;
  
          System.out.println("guess a number between 1 and 100");
  
          boolean isCorrect = false;
          for (int i = 1; i &lt;= 10; i++) {
              int input = sc.nextInt();
              if (input &gt; random_num)
                  System.out.println("It is less than " + input);
              else if (input &lt; random_num)
                  System.out.println("It is more than " + input);
              else {
                  isCorrect = true;
                  break;
              }
          }
  
          if (isCorrect)
              System.out.println("Congragulation you have guessd the correct number i.e " + random_num);
          else
              System.out.println("Game over it was " + random_num);
      }
  }
  </code></pre>
  
  <p>But I get errors here is the output:</p>
  
  <pre class="lang-none prettyprint-override"><code>guess a number between 1 and 100 
  It is more than 10 
  
  Exception in thread "main" java.util.NoSuchElementException 
      at java.base/ java.util.Scanner.throwFor(Scanner.java: 937) 
      at java.base/ java.util.Scanner.next(Scanner.java: 1594) 
      at java.base/ java.util.Scanner.nextInt(Scanner.java: 2258) 
      at java.base/ java.util.Scanner.nextInt(Scanner.java: 2212) 
      at Program.main(Program.java:15) 
  </code></pre>