I have a program that creates a GUI with three text fields in it, and two of these fields is supposed to accept a numeric value. I want to know how to get the numeric entries and store them in a variable for further calculations please.
I know how to get the string value entered into the one field, you just need to code,
String varname = txt1.getText();
and I was hopig for something similar for number values, a double to be specific, as there does not seem to be any .getDouble(); available??
If someone could advise me on this, I would be forever in your debt, and thanks in advance!!

You are looking for Double.parseDouble(String text), a static method of the java.lang.Double class, one of the subclasses of java.lang.Number. There are many similar methods such as Integer.parseInt, Float.parseFloat, Long.parseLong, and Byte.parseByte.

They all expect the string that you provide to contain exactly a numeral that you want to convert into a number. If they find anything else, they will throw a NumberFormatException and you should be prepared to deal with that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.