3
I am trying to check if a String x corresponds to a real number (any one). For this I created the following method:
public static boolean Real(String s, int i) {
boolean resp = false;
//
if ( i == s.length() ) {
resp = true;
} else if ( s.charAt(i) >= '0' && s.charAt(i) <= '9' ) {
resp = Real(s, i + 1);
} else {
resp = false;
}
return resp;
}
public static boolean isReal(String s) {
return Real(s, 0);
}
But obviously this method only serves to whole, and not real. Could you suggest me how to do this check?
P.S.: I can only use the functions: s.charAt(int)
and length()
java.
When you say "real" you mean decimal numbers, like
1,23
? Or you want something like floating point, which is accepted by the language itself (like1.23
, or even1.2e3
?) and later can be converted into a number? I gave my answer to the simplest case, but it can be adapted if necessary.– mgibsonbr
Yes, numbers like 4.0, 5.1, 1651.9
– bl4ck code