Here is my task. I have no idea how to implement this method.
Implement a method of Integer parseInt (String str) throws NumberFormatExceptionwhich will take the input string, which must contain onlynumbers and do not start from zero, and return a number, which must beobtained from the conversion of the line. not allowedthe use of methods of default classes Java. that's how I solved it:
public class Pars {
public static void main(String[] args) {
String s = " ";
int res = 0;
int[] arr = new int[s.length()];
try {
for (int i = 0; i < s.length(); i++) {
arr[i] = s.trim().charAt(i);
}
try {
for (int i = arr.length - 1, j = 1; i >= 0; i--, j *= 10) {
if ((arr[0] - 48) <= 0 || (arr[i] - 48) >= 10) {
throw new NumberFormatException();
} else {
res += (arr[i] - 48) * j;
}
}
System.out.println(res);
} catch (NumberFormatException n) {
System.out.println("Enter only numbers .");
}
} catch (StringIndexOutOfBoundsException str) {
System.out.println("Don't enter a space!");
}
}
}