Convert Height to Centimeter in Java -
i trying convert height in centimeter code works when have height eg: 4'5'' - 134cm
splitting text using split("'")
, it's working fine.
public double converttocms(string str){ double c = 0; string[] x = str.split("'"); try{ c = (integer.parseint(x[0].trim()) * 12 + integer.parseint(x[1].trim().split("\"")[0])) * 2.54; }catch(exception e){ e.printstacktrace(); } return c; }
but problem occurs when have height 6' - 182cm
because in x[1] -182
gets passed. 1 me out in such scenario.
public class so35575713 { public static void main(string[] args) { system.out.println(converttocms("6'")); } public static double converttocms(string str){ double c = 0; string[] x = str.split("'", 2); if(x[1].trim().isempty()){ x[1] = "0"; } try{ c = (integer.parseint(x[0].trim()) * 12 + integer.parseint(x[1].trim().split("\"")[0])) * 2.54; }catch(exception e){ e.printstacktrace(); } return c; } }
Comments
Post a Comment