This is the first solution to extract characters and numbers from an alpha numeric value into separate strings. This program uses the Character class methods.
import java.util.Scanner;
public class AlphanumericStr {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner inpAlphaNumeric = new Scanner (System.in);
StringBuilder strNumbers=new StringBuilder();
StringBuilder strLetters=new StringBuilder();
System.out.println("Enter an alpha numeric string (Ex:- 381BY4kjLpq6)");
String inpStr=inpAlphaNumeric.next();
int idx=0,strLen=inpStr.length();
while (--strLen >= 0) {
if(Character.isAlphabetic(inpStr.charAt(idx)))
strLetters.append(inpStr.charAt(idx));
else
if(Character.isDigit(inpStr.charAt(idx)))
strNumbers.append(inpStr.charAt(idx));
idx++;
}
inpAlphaNumeric.close();
System.out.println("The string of characters extracted from "+inpStr+" is="+strLetters.toString());
System.out.println("The string of numbers extracted from "+inpStr+" is="+ Integer.valueOf(strNumbers.toString()));
System.out.println("Add 1 to the Extracted number "+Integer.valueOf(strNumbers.toString())+" + "+"1="+(Integer.valueOf(strNumbers.toString())+1));
}
}
OUTPUT:
Enter an alpha numeric string (Ex:- 381BY4kjLpq6)
tl920Kjq4Gb
The string of characters extracted from tl920Kjq4Gb is=tlKjqGb
The string of numbers extracted from tl920Kjq4Gb is=9204
Add 1 to the Extracted number 9204 + 1=9205

Leave a comment