You shouldn't use keywords such as "Integer" for variable identifiers.
There are some lingering questions I have, but I would simply use Java's builtin split() function on strings and use regex to split the string into significant figures (ignoring ending and leading zeroes). Then return the sum of the length of the strings . Here is the code for pure integers.
String myint = "000100101111220000020000";
String[] sig_figs = myint.split("(^0+|0+$)");
int sum = 0;
for(String fig : sig_figs)
{
sum += fig.length();
}
return sum;
If we include floats, there are other rules you would have to consider, such as zeroes after a decimal point count except if they are preceded by all zeroes (3.00 has 3, 0.03 has 1). In this case, the regex regrettably becomes much more contrived, but I would use something like "(^0+(\\.?)0*|(~\\.)0+$|\\.)"
to split on. See code below (Note -- still works on integers)
String myfloat = "0.0120";
String [] sig_figs = myfloat.split("(^0+(\\.?)0*|(~\\.)0+$|\\.)");
int sum = 0;
for (String fig : sig_figs)
{
sum += fig.length();
}
return sum;
Here's a quick look into the regex:
^0+(\\.?)0*
means one or more leading zeroes (0+
) followed optionally by a decimal (\\.?
) and then zero or more zeroes (0*
).
(~\\.)0+$
means we want to take off ending zeroes (0+$
), but only if they aren't preceded by a decimal ((~\\.)
).
\\.
means split whatever is left by the decimal point.