Example usage for java.util Scanner nextInt

List of usage examples for java.util Scanner nextInt

Introduction

In this page you can find the example usage for java.util Scanner nextInt.

Prototype

public int nextInt(int radix) 

Source Link

Document

Scans the next token of the input as an int .

Usage

From source file:Main.java

public static void main(String[] args) {

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

    Scanner scanner = new Scanner(s);

    while (scanner.hasNext()) {

        // if the next is a int, print found and the int with radix 4
        if (scanner.hasNextInt()) {
            System.out.println("Found :" + scanner.nextInt(4));
        }/*from  w w  w . j a va  2  s.com*/
        System.out.println("Not Found :" + scanner.next());
    }

    scanner.close();
}

From source file:com.siphyc.utils.Utilities.java

public static boolean isInteger(String s, int radix) {
    Scanner sc = new Scanner(s.trim());
    if (!sc.hasNextInt(radix)) {
        return false;
    }/*  www .j ava  2 s .c om*/
    // we know it starts with a valid int, now make sure
    // there's nothing left!
    sc.nextInt(radix);
    return !sc.hasNext();
}