Scanner new Int Scan - Java java.util

Java examples for java.util:Scanner

Description

Scanner new Int Scan

Demo Code


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

public class Main {
    public static int newIntScan(Scanner scan, String print) //Allows user to enter nothing (a blank value) as well as something.
    {//from w  ww  .  j av  a 2  s  . c o m
        int number = -1;
        boolean run = true;
        String scanVal = null;
        String satisfy;
        while (run) {
            System.out.print(print);
            scanVal = scan.next();
            if (scanVal.equals("")) {
                number = -1;
                run = false;
            } else {
                try {
                    number = Integer.parseInt(scanVal);
                    run = false;
                } catch (Exception ex) {
                    System.out
                            .println("Please enter an integer (or nothing).");
                    satisfy = scan.next();
                    run = true;
                }
            }
        }
        return number;
    }
}

Related Tutorials