i'm working with lambda expressions and tried to make a generic functional interface where the interface takes a double value and returns that into an integer value. But then i get an error NumberFormatException or cannot cast error. If i use an method like (int)(Math.random) then it casts the double values perfectly into a integer, but here is my code and the errors i get:
package Exercise;
public class Training {
public static void main(String[] args) {
Training4<Integer, Double> obj = (x) -> Integer.parseInt(x.toString());
System.out.println(obj.constrmeth(10.5));
}
}
and here is the functional interface:
package Exercise;
public interface Training4 <T, S>{
T constrmeth(S a);
}
Error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "10.5"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Exercise.Training.lambda$0(Training.java:9)
at Exercise.Training.main(Training.java:10)