Scanner new Double Scan - Java java.util

Java examples for java.util:Scanner

Description

Scanner new Double Scan

Demo Code


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

public class Main {
    public static double newDoubleScan(Scanner scan, String print) //Allows user to enter nothing (a blank value) as well as something.
    {/*from  w w w.  j a v a2 s.c  o m*/
        double 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 = Double.parseDouble(scanVal);
                    run = false;
                } catch (Exception ex) {
                    System.out
                            .println("Please enter a number (or nothing).");
                    satisfy = scan.next();
                    run = true;
                }
            }
        }
        return number;
    }
}

Related Tutorials