Example usage for java.util Scanner hasNextInt

List of usage examples for java.util Scanner hasNextInt

Introduction

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

Prototype

public boolean hasNextInt(int radix) 

Source Link

Document

Returns true if the next token in this scanner's input can be interpreted as an int value in the specified radix using the #nextInt method.

Usage

From source file:Main.java

public static void main(String[] args) {

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

    Scanner scanner = new Scanner(s);

    while (scanner.hasNext()) {
        // check if the scanner's next token is an int with a radix of 4
        System.out.println(scanner.hasNextInt(4));

        System.out.println(scanner.next());
    }//  w  w  w.j a  va 2s  . c o  m
    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;
    }/*from   www  . j a v a 2  s. com*/
    // we know it starts with a valid int, now make sure
    // there's nothing left!
    sc.nextInt(radix);
    return !sc.hasNext();
}