I want the user to be able to enter a number all the way up to base 36 without allowing them to enter special characters in the string. So far this works, the only problem is it is not recursive and the return is based off the initialized r, instead of the r that is composed inside the loop.
I stepped through the debugger and just as I verified the string and I reached the end of the loop; just before the return, The value of r changed back to "". Instead of the right string.
I would like to know ho this could become recursive, but I need to find out why the return value gets re-initialized when leaving the loop.
The Method
public String numberVerification() {
String s = new Scanner(System.in).nextLine();
int ok = s.length();
String r = "";
for (int i = 0; i < ok; i++) {
char c = s.charAt(0);
if ((c > 0 && c < 48) || (c > 57 && c < 65) || (c > 90 && c < 97)
|| (c > 122 && c <= 127)) {
System.out.print("The number you have entered is invalid!"
+ "\nTry again: ");
numberVerification();
}
r = r + Character.toString(c);
s = s.substring(1, s.length());
if (s == "")
ok = 0;
}
return r;
}

New Topic/Question
Reply



MultiQuote







|