Search code examples
javalambdaprojectpalindromecomposite

Java multiple inputs lead to an exception


When I input in in a "1 2" format it gives error if in "1 enter 2" It gives error

I'm tasked to create a lambda that has a function that deals with knowing what the user input is. The user would chose to determine either odd, even, prime, composite or palindrome. But I cant use this as a reference as it doesn't give any instruction on how to use it.

The first one gives

Exception in thread "main" java.lang.NumberFormatException: For input string: "4 1"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
    at java.base/java.lang.Integer.parseInt(Integer.java:652)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)
    at Lambda_Project/Project.Solution.main(Solution.java:45)

The second one is

Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.StringTokenizer.nextToken(StringTokenizer.java:348)
    at Lambda_Project/Project.Solution.main(Solution.java:53)

Here's the Code ...

import java.io.*;
    import java.util.*;
    interface PerformOperation {
        boolean check(int a);
    }
    class MyMath {
        public boolean checker(PerformOperation p, int num) {
            return p.check(num);
        }
    
        public PerformOperation is_odd() {
            return n -> (n & 1) == 1;
        }
    
        public PerformOperation is_prime() {
            // O(n^(1/2)) runtime
            return n -> {
                if (n < 2) {
                    return false;
                }
                int sqrt = (int) Math.sqrt(n);
                for (int i = 2; i <= sqrt; i++) {
                    if (n % i == 0) {
                        return false;
                    }
                }
                return true;
            };
        }
    
        public PerformOperation is_palindrome() {
            return n -> {
                String original = Integer.toString(n);
                String reversed = new StringBuilder(Integer.toString(n)).reverse().toString();
                return original.equals(reversed);
            };
        }
    }
    
    public class Solution {
        public static void main(String[] args) throws IOException {
            MyMath ob = new MyMath();
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            int T = Integer.parseInt(br.readLine());  // IT ERRORS HERE
            PerformOperation op;
            boolean ret = false;
            String ans = null;
            while (T--> 0) {
                String s = br.readLine().trim();
                StringTokenizer st = new StringTokenizer(s);
                int ch = Integer.parseInt(st.nextToken());
                int num = Integer.parseInt(st.nextToken()); // AND HERE
                if (ch == 1) {
                    op = ob.is_odd();
                    ret = ob.checker(op, num);
                    ans = (ret) ? "ODD" : "EVEN";
                } else if (ch == 2) {
                    op = ob.is_prime();
                    ret = ob.checker(op, num);
                    ans = (ret) ? "PRIME" : "COMPOSITE";
                } else if (ch == 3) {
                    op = ob.is_palindrome();
                    ret = ob.checker(op, num);
                    ans = (ret) ? "PALINDROME" : "NOT PALINDROME";
                }
                System.out.println(ans);
            }
        }
    }

Solution

  • "1 2" is not a string representing an int and therefore, it can not be parsed into an int.

    You need to split the such an input and then you can parse the individual elements into int values and perform the required arithmetic operation on them e.g.

    public class Main {
        public static void main(String[] args) {
            String input = "1 2";
            String[] arr = input.split("\\s+");// Split on whitespace
            for (String s : arr) {
                System.out.println(s + " + 10 = " + (Integer.parseInt(s) + 10));
            }
        }
    }
    

    Output:

    1 + 10 = 11
    2 + 10 = 12