我不知道为什么这个字符串无法解析。我尝试将一个字符串数组解析为一个int的数组。感谢你的帮助。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int input =sc.nextInt();
String[] b = Integer.toBinaryString(input).split("[0]+");
int [] intArray = new int[b.length];
for (int i = 0; i < intArray.length; i++) {
//this where the error seems to be
intArray[i]= Integer.parseInt(b[i]);
System.out.println(intArray[i]);
}
int highestConsecutiveOnes = intArray[0];
for (int i = 0; i < intArray.length; i++) {
if (intArray[i]> highestConsecutiveOnes){
highestConsecutiveOnes= intArray[i];
}
}
int [] highestConsecutiveOnesArray = Integer.toString(highestConsecutiveOnes).chars().map(c -> c -= '0').toArray();
int sumOfMostConsecutiveOnes = Arrays.stream(highestConsecutiveOnesArray).sum();
System.out.println(sumOfMostConsecutiveOnes);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int input =sc.nextInt();
String[] b = Integer.toBinaryString(input).split("[0]+");
int [] intArray = new int[b.length];
for (int i = 0; i < intArray.length; i++) {
//this where the error seems to be
intArray[i]= Integer.parseInt(b[i]);
System.out.println(intArray[i]);
}
int highestConsecutiveOnes = intArray[0];
for (int i = 0; i < intArray.length; i++) {
if (intArray[i]> highestConsecutiveOnes){
highestConsecutiveOnes= intArray[i];
}
}
int [] highestConsecutiveOnesArray = Integer.toString(highestConsecutiveOnes).chars().map(c -> c -= '0').toArray();
int sumOfMostConsecutiveOnes = Arrays.stream(highestConsecutiveOnesArray).sum();
System.out.println(sumOfMostConsecutiveOnes);
}
它返回NumberFormatException
Exception in thread "main" java.lang.NumberFormatException: For input string: "1111111111111111"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at testrun.Tester.main(Tester.java:19)