# Recursive Method to Verify Alpha/Numeric Numbers

Page 1 of 1

## 4 Replies - 1379 Views - Last Post: 12 March 2011 - 11:07 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=221770&amp;s=f36b136a69ec180a8df63e38642cce34&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 x68zeppelin80x

Reputation: 130
• Posts: 576
• Joined: 07-March 09

# Recursive Method to Verify Alpha/Numeric Numbers

Posted 12 March 2011 - 05:51 PM

I am trying to figure out a way for the user to enter a string and then have the code recursively verify each character in the string. The user can only have 0 --> 9, a --> z, && A --> Z in the string.

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;
}

```

Is This A Good Question/Topic? 0

## Replies To: Recursive Method to Verify Alpha/Numeric Numbers

### #2 x68zeppelin80x

Reputation: 130
• Posts: 576
• Joined: 07-March 09

## Re: Recursive Method to Verify Alpha/Numeric Numbers

Posted 12 March 2011 - 06:10 PM

EDIT: This WORKS! - Just need recursion... - Thanks

```public String numberVerification() {
String s = new Scanner(System.in).nextLine();
for (int i = 0; i < s.length(); i++) {
char c = (s.charAt(i));
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: ");
s = numberVerification();
}
}
return s;
}

```

I forgot to set numberVerification() --> s

This post has been edited by x68zeppelin80x: 12 March 2011 - 06:17 PM

### #3 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12241
• Posts: 45,327
• Joined: 27-December 08

## Re: Recursive Method to Verify Alpha/Numeric Numbers

Posted 12 March 2011 - 06:24 PM

Your validation should be separate from your user interface. Beyond that, "recursive iteration" is the way to go. By that, I mean something like this:
```public boolean validate(String s, int index){
//when index == s.length(), return true
//because none of the characters failed validation

//otherwise, validate the character at index
//then return the recursive call, incrementing index
}

```

Basically, b/c we're incrementing an index variable, the recursive method acts as a for loop. Not really a great use of recursion, but if that's your assignment.

### #4 x68zeppelin80x

Reputation: 130
• Posts: 576
• Joined: 07-March 09

## Re: Recursive Method to Verify Alpha/Numeric Numbers

Posted 12 March 2011 - 06:27 PM

x68zeppelin80x, on 12 March 2011 - 06:10 PM, said:

EDIT: This WORKS! - Just need recursion... - Thanks

```public String numberVerification() {
String s = new Scanner(System.in).nextLine();
for (int i = 0; i < s.length(); i++) {
char c = (s.charAt(i));
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: ");
s = numberVerification();
}
}
return s;
}

```

I forgot to set numberVerification() --> s

s = numberVerification() == return numberVerification()

WOW, I feel like an idiot... Sorry for making you guys think for nothing. I love posting and then finding the answer out myself. This is almost like asking someone to assist you in finding something, and when they start to help you, you have already found it. Anyways thanks for helping if you were going to.

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Recursive Method to Verify Alpha/Numeric Numbers

Posted 12 March 2011 - 11:07 PM

Or something like this

```	private boolean isValid(String digit) {

// valid first digit
char c = digit.charAt(0);
boolean valid = c >= '0' &&  c <= '9'
|| c >= 'A' && c <= 'Z'
|| c >= 'a' && c <= 'z';
// one digit not valid no neeed to continue
if(!valid)
return false;
// if it is the last to check return true it is valid
if(digit.length() == 1)
return true;
// test with next element
return isValid(digit.substring(1));
}

```