Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read in a double in Java?

Tags:

java

double

input

I am trying to get a decimal input from the keyboard, and it is just not working. First I tried

double d = Integer.parseInt(JOptionPane.showInputDialog(
                                    "Please enter a number between 0 and 1:"));

and that obviously didn't work very well.

I am used to just parsing int's as they come in from the keyboard right into a variable, but I don't know what I am supposed to do for decimals! I need to be able to take a decimal like .9 straight from the keyboard and be able to have it in a variable I can do calculations with.

I know this is a basic question, but I need some help. Thanks!

like image 699
Jeff Avatar asked Dec 02 '22 06:12

Jeff


1 Answers

I would use the Double class rather than the Integer class

double d = Double.parseDouble((String)JOptionPane.showInputDialog("Please enter a number between 0 and 1:"));
like image 183
John Weldon Avatar answered Dec 06 '22 09:12

John Weldon