Skip to content

Commit c5bf1a2

Browse files
Create 41.1 Functional Interface and Lamda Expression.java
1 parent da077e9 commit c5bf1a2

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Problem
3+
Submissions
4+
Leaderboard
5+
Create a functional interface with the following method in it: boolean valid(String str, int n);
6+
7+
Write a program with the help of lambda expression that will use this valid() method to find out if a string (str) is having characters greater than "n" in it or not.
8+
9+
Input Format
10+
11+
Your program will take 2 types of inputs. First input will be a string (i.e. value for "str") and second input will be no. of characters to compare (i.e. value for "n"). If value of "n" entered less than 1, then display message "Invalid input". Otherwise, display message "Valid string" if string is having characters greater than "n" or display message "Invalid string" if string is having characters less than "n".
12+
13+
Constraints
14+
15+
No. of characters (i.e. "n") should be greater than 0.
16+
17+
Output Format
18+
19+
Display message "Valid string" if string is having characters greater than "n" or display message "Invalid string" if string is having characters less than "n".
20+
21+
Sample Input 0
22+
23+
Lovely
24+
4
25+
Sample Output 0
26+
27+
Valid string
28+
*/
29+
30+
import java.util.*;
31+
32+
public class Main {
33+
34+
// Functional interface with valid() method
35+
interface StringValidator {
36+
void valid(String str, int n);
37+
}
38+
39+
public static void main(String[] args) {
40+
41+
// Lambda expression to check if string has characters greater than "n"
42+
StringValidator validator = (str, n) -> {
43+
if (n < 1)
44+
{
45+
System.out.println("Invalid input");
46+
}
47+
else if (str.length() > n)
48+
{
49+
System.out.println("Valid string");
50+
}
51+
else
52+
{
53+
System.out.println("Invalid string");
54+
}
55+
};
56+
Scanner sc = new Scanner(System.in);
57+
String str = sc.nextLine();
58+
int n = sc.nextInt();
59+
validator.valid(str,n);
60+
61+
62+
}
63+
}

0 commit comments

Comments
 (0)