java - Having converting and getting data problems -
i wrote program calculates acceleration. how looks:
when press button show dialog message part of joptionpane
. data enter in fields strings convert them double doing :
double name = double.valueof(string);
the problem if inserted string
value instead of double value in fields , pressed button sends acceleration total program badly crash. need solution fix that!
if said why did set inserted value string
answer way
textfieldname.gettext();
if knew way double text field please tell me it.
try this:
public static void main(string[] args) { string[] values = {"1.0", "2.0", "a"}; (string value: values) { system.out.println(getvalue(value)); } } private static double getvalue(string valuestring) { double result; try { result = double.valueof(valuestring); } catch (numberformatexception e) { result = 0.0; } return result; }
Comments
Post a Comment