Cloud Stack Ninja

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)

Im still a 2nd year student so please go easy on me

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);
            }
        }
    }


Read more here: https://stackoverflow.com/questions/64397322/this-is-a-example-code-of-my-prof-to-use-as-a-reference-for-our-project-but-i-do

Content Attribution

This content was originally published by Inqueseraux at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: