An inexplicable problem in processing params Follow
Hi, there.
I have an inexplicable problem in processing params.
This is my method code block,
import java.util.HashMap;
import java.util.Map;
class TwoSum_Solution {
public int[] twoSum(int[] nums, int t) {
Map<Integer, Integer> map = new HashMap<>();
int n = nums.length;
for (int i = 0; i < n; ++i) {
int b = t - nums[i];
if (map.containsKey(b)) {
return new int[]{map.get(b), i};
}
map.put(nums[i], i);
}
return new int[]{};
}
}
And this is my testing class:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class _0001_TwoSumTest {
public static int[] stringToIntegerArray(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return new int[0];
}
String[] parts = input.split(",");
int[] output = new int[parts.length];
for (int index = 0; index < parts.length; index++) {
String part = parts[index].trim();
output[index] = Integer.parseInt(part);
}
return output;
}
public static String integerArrayToString(int[] nums, int length) {
if (length == 0) {
return "[]";
}
StringBuilder result = new StringBuilder();
for (int index = 0; index < length; index++) {
int number = nums[index];
result.append(number).append(", ");
}
return "[" + result.substring(0, result.length() - 2) + "]";
}
public static String integerArrayToString(int[] nums) {
return integerArrayToString(nums, nums.length);
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
int[] nums = stringToIntegerArray(line);
line = in.readLine();
int target = Integer.parseInt(line);
int[] ret = new TwoSum_Solution().twoSum(nums, target);
String out = integerArrayToString(ret);
System.out.print(out);
}
}
}
All the code comes from:
https://leetcode.com/problems/two-sum/
should be okay.
Here's the situation 1:
When I run my testing class, I input:
[1,2,3]
then press "enter", then input:
5
and press again I received:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:678)
at java.base/java.lang.Integer.parseInt(Integer.java:786)
at _0001_TwoSumTest.main(_0001_TwoSumTest.java:45)
Process finished with exit code 1
this probliem only occurs on my laptop, but not on my PC.
Here's the situation 2:
But if I input this on my laptop:
[1,2,3]
5
then press "enter" I received correct output:
[1, 2]
I am really wondering why.
I wish my laptop works well on situation 1 and returns no exception.
Could anyone help? I'd be really thankful!
Please sign in to leave a comment.
Hi,
It is a known issue: https://youtrack.jetbrains.com/issue/IDEA-293951
Sorry that you had to bump into it.
The fix will be rolled out in version 2022.1.2 due for release on May 31.
Got it, thanks.