title: How do I read formatted input in Java?
body: <p>Suppose my input file contains:</p>
  
  <pre><code>3 4 5 6    7        8
  9
  
  
  10
  </code></pre>
  
  <p>I want to run a while loop and read integers, so that I will get 3,4,5,6,7,8 and 10 respectively after each iteration of the loop.</p>
  
  <p>This is really simple to do in C/C++ but not in Java...</p>
  
  <p>I tried this code:</p>
  
  <pre><code>try {
              DataInputStream out2 = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
  
              int i=out2.read();
              while(i!=-1){
                  System.out.println(i);
                  i=out2.readInt();
              }
  
      } catch (IOException ex) {
  
      }
  </code></pre>
  
  <p>and what I get is:</p>
  
  <pre><code>51
  540287029
  540418080
  538982176
  151599117
  171511050
  218762506
  825232650
  </code></pre>
  
  <p>How do I read the integers from this <strong>file</strong> in <strong>Java</strong>?</p>