英文:
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 "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);
}
}
}
答案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
英文:
"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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论