我试图将数字输入为string
,然后将其拆分并存储在string
数组中,然后再将它们作为整数存储在int
数组中。我已经尝试了许多类似.trim()
或scanner.skip()
的方法,但是无法在此处解决此问题。
我的输入:
4
1 2 2 2
public static void main(String []args) throws IOException{
Scanner scanner = new Scanner(System.in);
int arCount = scanner.nextInt();
int[] ar = new int[arCount];
String[] arItems = scanner.nextLine().split(" ");
for (int i = 0; i < arCount; i++) {
int arItem = Integer.parseInt(arItems[i].trim());
ar[i] = arItem;
}
scanner.close();
}
收到的错误是:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at Hello.main(Hello.java:32)
答案 0 :(得分:0)
您可以捕获异常并跳过不良记录。
我还建议您将一个或多个空格分开,以防止空字符串开头
Scanner scanner = new Scanner(System.in);
int arCount = scanner.nextInt();
scanner.nextLine();
int[] ar = new int[arCount];
String[] arItems = scanner.nextLine().split("\\s+");
for (int i = 0; i < arCount; i++) {
try {
ar[i] = Integer.parseInt(arItems[i].trim());
} catch (NumberFormatException e) {
System.err.println(arItems[i] + " is not an int");
}
}
答案 1 :(得分:0)
为什么不一次又一次地使用nextInt()方法(完全跳过split的使用),就像这样,
import java.util.*;
public class MyClass {
public static void main(String []args){
Scanner scanner = new Scanner(System.in);
int arCount = scanner.nextInt();
int[] ar = new int[arCount];
for(int i=0;i<arCount;i++){
ar[i] = scanner.nextInt();;
}
System.out.println(Arrays.toString(ar));
scanner.close();
}
}
更新:
在您的原始程序中,您首先使用nextInt()方法,该方法不使用换行符'\ n'。因此,nextLine()的下一次调用实际上消耗了同一行上的换行符,而不是包含数组整数的下一行。因此,为使代码正常工作,您可以按如下所示对其进行一些修改,
import java.util.*;
public class MyClass {
public static void main(String []args){
Scanner scanner = new Scanner(System.in);
int arCount = scanner.nextInt(); // doesn't consume \n
int[] ar = new int[arCount];
scanner.nextLine(); //consumes \n on same line
String line = scanner.nextLine(); // line is the string containing array numbers
System.out.println(line);
String[] arItems = line.trim().split(" "); //trim the line first, just in case there are extra spaces somewhere in the input
System.out.println(Arrays.toString(arItems));
for (int i = 0; i < arCount; i++) {
int arItem = Integer.parseInt(arItems[i].trim());
ar[i] = arItem;
}
System.out.println(Arrays.toString(arItems));
}
}
答案 2 :(得分:-2)
也许可以在Scanner.nextLine()之后尝试使用.trim()。
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
function generateRandomNumbers(maxNumber, randomNumbersCount) {
var possibleNumbers = [];
// populate array with all possible values
for (var i = 0; i <= maxNumber; i++) { possibleNumbers.push(i); }
// shuffle the array to get a random order of the possible numbers O(n)
shuffleArray(possibleNumbers);
// trim the array down to only the first n numbers where n = randomNumbersCount
possibleNumbers.length = randomNumbersCount;
return possibleNumbers;
}
console.log (generateRandomNumbers(10, 5));
console.log (generateRandomNumbers(10, 5));
console.log (generateRandomNumbers(10, 5));