Java API Tutorial - Java Scanner.nextInt()








Syntax

Scanner.nextInt() has the following syntax.

public int nextInt()

Example

In the following code shows how to use Scanner.nextInt() method.

/*ww w .j  a  v a2 s  . c  o m*/
import java.util.Scanner;

public class Main {

   public static void main(String[] args) {

      String s = "java2s.com 1 + 1 = 2.0 true ";

      Scanner scanner = new Scanner(s);

      while (scanner.hasNext()) {

         if (scanner.hasNextInt()) {
            System.out.println("Found :" + scanner.nextInt());
         }
         System.out.println("Not Found :" + scanner.next());
      }


      scanner.close();
   }
}

The code above generates the following result.