Java Scanner Usage getInt()

Here you can find the source of getInt()

Description

Returns an int from standard input, ignoring input that is not an int

License

Open Source License

Return

An int from standard input

Declaration

public static int getInt() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
    private static final Scanner keyboard = new Scanner(System.in);

    /**/*from w  w  w  . j  av a 2  s .  c o  m*/
     * Returns an int from standard input, ignoring input that is not an int
     * @return An int from standard input
     */
    public static int getInt() {
        int anInt = 0;

        try {
            anInt = keyboard.nextInt();
        } catch (InputMismatchException ex) {
            //ignore exception, prompt user again for input if input is incorrect
        }
        keyboard.nextLine();//clear the buffer

        return anInt;
    }
}

Related

  1. getDirectoryString()
  2. getEndLine(String cqlString)
  3. getExtensions(Scanner scanner)
  4. getFirstInt(String s)
  5. getInput(Scanner in, String message, boolean allowNull)
  6. getInt(Scanner sc)
  7. getInteger(String parameter)
  8. getLines(String str)
  9. getLineStartingWith(Scanner scanner, String starts)