Java多个输入导致异常。

huangapple go评论75阅读模式
英文:

Java multiple inputs lead to an exception

问题

当我以 "1 2" 格式输入时,如果以 "1 enter 2" 格式输入会产生错误。

我被任务要创建一个具有处理了解用户输入的函数的 lambda 函数。
用户可以选择确定奇数、偶数、素数、合数或回文。但我不能将此作为参考,因为它没有提供任何关于如何使用它的指示。

第一个错误信息是

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)

第二个错误信息是

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)

以下是代码 ...

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);
        }
    }
}
英文:

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 &quot;main&quot; java.lang.NumberFormatException: For input string: &quot;4 1&quot;
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 &quot;main&quot; 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 -&gt; (n &amp; 1) == 1;
}
public PerformOperation is_prime() {
// O(n^(1/2)) runtime
return n -&gt; {
if (n &lt; 2) {
return false;
}
int sqrt = (int) Math.sqrt(n);
for (int i = 2; i &lt;= sqrt; i++) {
if (n % i == 0) {
return false;
}
}
return true;
};
}
public PerformOperation is_palindrome() {
return n -&gt; {
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--&gt; 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) ? &quot;ODD&quot; : &quot;EVEN&quot;;
} else if (ch == 2) {
op = ob.is_prime();
ret = ob.checker(op, num);
ans = (ret) ? &quot;PRIME&quot; : &quot;COMPOSITE&quot;;
} else if (ch == 3) {
op = ob.is_palindrome();
ret = ob.checker(op, num);
ans = (ret) ? &quot;PALINDROME&quot; : &quot;NOT PALINDROME&quot;;
}
System.out.println(ans);
}
}
}

答案1

得分: 0

"1 2"不是表示int的字符串,因此无法解析为int

你需要分割这样的输入,然后可以将各个元素解析为int值,并对它们执行所需的算术运算,例如:

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

输出:

1 + 10 = 11
2 + 10 = 12
英文:

&quot;1 2&quot; 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 = &quot;1 2&quot;;
String[] arr = input.split(&quot;\\s+&quot;);// Split on whitespace
for (String s : arr) {
System.out.println(s + &quot; + 10 = &quot; + (Integer.parseInt(s) + 10));
}
}
}

Output:

1 + 10 = 11
2 + 10 = 12

huangapple
  • 本文由 发表于 2020年10月17日 06:43:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64397322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定