How to find length of alpha numeric value or string

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

Comments

2 responses to “How to find length of alpha numeric value or string”

  1. I just could not depart your web site before suggesting that I actually enjoyed the usual information a person supply in your visitors? Is going to be back incessantly to check up on new posts

    Like

  2. You actually make it seem so easy with your presentation but I find this matter to be actually something which I think I would never understand. It seems too complex and very broad for me. I am looking forward for your next post, I will try to get the hang of it!

    Like

Leave a comment