read double, but if blank input do something else[java]

Started by
1 comment, last by Nanook 15 years, 6 months ago
I want to ask the user for a double value and read it in using nextDouble() method in the Scanner class from the keyboard.. but if the user just hits enter I want to register that aswell.. how can I do this as easy as possible?
Advertisement
Hmm, I don't know much about Java, but Google gave me this(http://www.java2s.com/Code/JavaAPI/java.util/ScannernextDouble.htm):
...else if (src.hasNextDouble()) {        d = src.nextDouble();...


So try Scanner.hasNextDouble to check whether the input is a double or something else, or nothing (Scanner.hasNext).
Yeah, well.. I dont think that would work from the keyboard though? I came up with a way to do it though.. I read in a nextLine instead.. (String) then checked if that was empty and then if it wasn't I tried to parse it to a double..

This topic is closed to new replies.

Advertisement