This is the second solution to find the length of an alpha numeric or string value, using functional interface and lambda expression without return statement. This approach uses direct String class length method.
import java.util.Scanner;
//Define functional interface lengthOfAlphanumeric
interface lengthOfAlphanumeric {
public int lenOfAlphanumeric(String strVal);
}
public class LenOfAlphanumeric {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner inpAlphaNum = new Scanner(System.in);
// Input a long integer
System.out.print("Enter an Alphanumeric value=");
String alphaNumeric = inpAlphaNum.next();
System.out.println("The alphanumeric value is=" + alphaNumeric);
// Define lambda expression laN which returns length
lengthOfAlphanumeric laN = alphaNum -> alphaNum.length();
// Call the lambda expression with parameter as alphanumeric
System.out.println("The Length of the alphanumeric value=" + alphaNumeric + " is=" + laN.lenOfAlphanumeric(alphaNumeric));
System.out.println("The Length of other alphanumeric value or string is=" + laN.lenOfAlphanumeric("GdIJJ98YT#21"));
System.out.println("The Length of other alphanumeric value or string is=" + laN.lenOfAlphanumeric("Drishya"));
}
}
OUTPUT:
Enter an Alphanumeric value=KyJ7#!92F
The alphanumeric value is=KyJ7#!92F
The Length of the alphanumeric value=KyJ7#!92F is=9
The Length of other alphanumeric value=12
The Length of other alphanumeric value=14

Leave a comment