Here you can find the source of readInt(String prompt)
Parameter | Description |
---|---|
prompt | the question to prompt the user |
public static int readInt(String prompt)
//package com.java2s; //License from project: Open Source License import java.util.Scanner; public class Main { /**//from w w w . j a v a 2s . c o m * Writes a prompt to standard out and tries to read an int value from * standard in. This is repeated until an int value is entered. * * @param prompt * the question to prompt the user * @return the first int value which is entered by the user */ public static int readInt(String prompt) { int value = 0; boolean intRead = false; do { System.out.print(prompt); try (Scanner line = new Scanner(System.in); Scanner scannerLine = new Scanner(line.nextLine());) { if (scannerLine.hasNextInt()) { intRead = true; value = scannerLine.nextInt(); } } } while (!intRead); return value; } }