Scanner input Number - Java java.util

Java examples for java.util:Scanner

Description

Scanner input Number

Demo Code


//package com.java2s;
import java.util.*;

public class Main {
    public static int inputNumber(Scanner scan) {
        int numberOfStudents = 0;
        boolean run = true;
        String satisfy;/*w  w w .  j a  v  a2s  .c om*/
        while (run) {
            try {
                System.out.print("Number of students to process: ");
                numberOfStudents = scan.nextInt();
                run = false;
            } catch (Exception ex) //Catch to make sure that the user inputs a integer for the number of students (not something else).
            {
                System.out.println("Please enter a number.");
                run = true;
                satisfy = scan.next(); //Satisfy the read.
            }
        }
        return numberOfStudents;
    }
}

Related Tutorials