/*
* Input: A14BC654 -> acceptable
* Input: AB54C54D -> not acceptable
* Input: A23BED75 -> acceptable
* Conditions to check:
* 1) First and Last index values of the string are character and digit respectively
* 2) If condition 1 is satisfies it prints last number value of the string. For example, the value 654 if we pass first String, should print 75 if we pass 3rd string.
* Note: 2nd string fails in 1st condition check itself
*/
METHOD 1:
public class NumbersInString {
public static void main(String[] args) {
String str="A14BC654";
String lastNumber="";
if(str.matches("[a-zA-Z].*[0-9]")) { // start condition check 1
for(int j=str.length()-1;j>=0;j--) {
if(Character.isDigit(str.charAt(j))) { // start condition check 2
lastNumber=lastNumber+str.charAt(j);
}else {
break;
} // closing else block
} // closing for loop
System.out.println("Last Number of given String is: "+lastNumber);
}else {
System.out.println("String "+str+" is not matched with first condition");
} // closing condition check 1
}
}
Output: 654
METHOD 2:
public class NumbersInString {
public static void main(String[] args) {
String str="A14BC654";
String lastNumber="";
for(int i=0;i<str.length()-1;i++) {
if(Character.isAlphabetic(str.charAt(0))&&Character.isDigit(str.charAt(str.length()-1))){
for(int j=str.length()-1;j>=0;j--) {
if(Character.isDigit(str.charAt(j))) {
lastNumber=lastNumber+str.charAt(j);
}else {
break;
}
}
System.out.println("Last Number of given String is: "+lastNumber);
break;
}else {
System.out.println("String "+str+" is not mached with first condiation");
}
}
}
Output: 654
* Input: A14BC654 -> acceptable
* Input: AB54C54D -> not acceptable
* Input: A23BED75 -> acceptable
* Conditions to check:
* 1) First and Last index values of the string are character and digit respectively
* 2) If condition 1 is satisfies it prints last number value of the string. For example, the value 654 if we pass first String, should print 75 if we pass 3rd string.
* Note: 2nd string fails in 1st condition check itself
*/
METHOD 1:
public class NumbersInString {
public static void main(String[] args) {
String str="A14BC654";
String lastNumber="";
if(str.matches("[a-zA-Z].*[0-9]")) { // start condition check 1
for(int j=str.length()-1;j>=0;j--) {
if(Character.isDigit(str.charAt(j))) { // start condition check 2
lastNumber=lastNumber+str.charAt(j);
}else {
break;
} // closing else block
} // closing for loop
System.out.println("Last Number of given String is: "+lastNumber);
}else {
System.out.println("String "+str+" is not matched with first condition");
} // closing condition check 1
}
}
Output: 654
METHOD 2:
public class NumbersInString {
public static void main(String[] args) {
String str="A14BC654";
String lastNumber="";
for(int i=0;i<str.length()-1;i++) {
if(Character.isAlphabetic(str.charAt(0))&&Character.isDigit(str.charAt(str.length()-1))){
for(int j=str.length()-1;j>=0;j--) {
if(Character.isDigit(str.charAt(j))) {
lastNumber=lastNumber+str.charAt(j);
}else {
break;
}
}
System.out.println("Last Number of given String is: "+lastNumber);
break;
}else {
System.out.println("String "+str+" is not mached with first condiation");
}
}
}
Output: 654
0 comments:
Post a Comment