これが私の仕事です。このメソッドを実装する方法がわかりません。
Integer parseInt (String str) throws NumberFormatException のメソッドを実装します。これは、数値のみを含み、ゼロから開始しない入力文字列を受け取り、行の変換から取得する必要がある数値を返します。許可されていないデフォルト クラス Java のメソッドの使用。それが私がそれを解決した方法です:
パブリック クラスのパース {
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!");
}
}
}