public class atoi { public static void main(String args[]) { char[] c = { '1', '0', '9', '4' }; int i = atoi(c); System.out.print(i); } private static int atoi(char[] c) { if (containsOthers(c)) return -1; int result = 0; for (int i = 0; i
public static void main(String args[]) {
char[] c = { '1', '0', '9', '4' };
int i = atoi(c); System.out.print(i);
}
private static int atoi(char[] c) {
if (containsOthers(c))
return -1; int result = 0;
for (int i = 0; i < c.length; i++) { result = result + (c[i] - '0')
* (int) (Math.pow(10, c.length - i - 1)); }
return result;
// return c[2]-'0'+(c[1]-'0')*10+(c[0]-'0')*100; }
private static boolean containsOthers(char[] c) {
for (int i = 0; i < c.length; i++) {
if (c[i] - '0' < 0 || c[i] - '0' > 9)
return true;
}
return false;
}}