title: Unable to handle infinite loop when using Scanner for input
body: <p>I have this piece of code :</p>
  
  <pre><code>import java.util.*;
  class HelloWorld {
      public static void main(String args[]) {
          Scanner ob = new Scanner(System.in);
          int t = ob.nextInt(); //no. of Test Cases
                  ob.next(); // for next input whether exception occurs or not
          int a = 0, c = 0;
          for (int j = 0; j &lt; t; j++)
          {
              a = 0; c = 0;
              String str = ob.nextLine();
              String [] spstr = str.split("\\s+");
              try
              {
                  for (int i=0 ; i&lt;spstr.length ; i++)
                  {
                      if(spstr[i].equals("")) {i--;}
                      else {
                          c = c + Integer.parseInt(spstr[i]);
                          }
                  }
                  System.out.println(c);
              } catch (Exception e) {
                  System.out.println("Invalid Input");
                  }
          }
      }
  }
  </code></pre>
  
  <p>What this code do is add any no.s of integers in a single line. Before doing this, I have a Test Case <code>int t</code>. This decides how many inputs must be taken. But this results to an infinite loop even when I am entering integer value.</p>
  
  <p>I have seen this post:
  <a href="https://stackoverflow.com/questions/3572160/how-to-handle-infinite-loop-caused-by-invalid-input-using-scanner">How to handle infinite loop caused by invalid input using Scanner</a> which have many answers on how to get rid of this. I have followed the answers, but I have not yet solved this issue.</p>
  
  <p><strong>Note:</strong> When I use <code>int t=5;</code>, it works fine. But in this case too, if exception is caught twice, same thing happens.</p>
  
  <p>Please tell me how to solve this infinite loop error ?</p>
  
  <p>Thanks in Advance :)</p>