I want to scan a String and its content. I want to return an error message if there are any character in the string. For example: int a = myFunction("123"); will save "123" in a, but when the user tries to do something like int a = myFunction("12s32"); it should return the error, because there is a character in the string. This is what i got so far:
public class Parseint {
public static int parseInt(String str) {
if (str == null || str.isEmpty()) {
System.out.println("Der String enthaelt keine Zeichen");
return -1;
} else {
int sum = 0;
int position = 1;
for (int i = str.length() - 1; i >= 0; i--) {
int number = str.charAt(i) - '0';
sum += number * position;
position = position * 10;
}
return sum;
}
}
public static void main(String[] args) {
int a = parseInt("");
System.out.println(a);
}
}