[英]Java charAt() String index out of range: 5
I am trying to figure out "what 5-digit number when multiplied by 4 gives you its reverse?" 我试图弄清楚“乘以4时得到的5位数是多少?” using this code but I get error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5 at java.lang.String.charAt(String.java:658) at Digits.main(Digits.java:12) 使用此代码,但我得到错误:线程“主”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:Digits.main(Digits.java)处java.lang.String.charAt(String.java:658)为5 :12)
public class Digits{
public static void main(String[] args) {
int n = 0;
int b = 0;
String number = Integer.toString(n);
String backwards = Integer.toString(b);
for (int x = 9999; x < 100000 ; x++ ) {
n = x;
b = x *4;
if (number.charAt(0) == backwards.charAt(5 )&& number.charAt(1) == backwards.charAt(4)
&& number.charAt(2) == backwards.charAt(3) && number.charAt(3) == backwards.charAt(2)
&& number.charAt(4) == backwards.charAt(1) && number.charAt(5) == backwards.charAt(0)) {
System.out.println(n);
break;
}
}
Any help would be grealy appreciated 任何帮助将不胜感激
Correct. 正确。 Because the first five characters are at indices 0, 1, 2, 3
and 4
. 因为前五个字符位于索引0, 1, 2, 3
和4
。 I would use a StringBuilder
(because of StringBuilder.reverse()
). 我将使用StringBuilder
(因为StringBuilder.reverse()
)。 And, I would suggest you restrict variable visibility. 而且,我建议您限制变量的可见性。 Then remember to modify number
and backwards
when you change n
and/or b
. 然后记得在更改n
和/或b
时修改number
并向backwards
修改。 Something like 就像是
for (int x = 9999; x < 100000; x++) {
int n = x;
int b = x * 4;
String number = Integer.toString(n);
String backwards = Integer.toString(b);
StringBuilder sb = new StringBuilder(number);
sb.reverse();
if (sb.toString().equals(backwards)) {
System.out.printf("%s * 4 = %s", number, backwards);
}
}
And I get 我得到
21978 * 4 = 87912
backwards
and number
are String
, which internally uses an array. backwards
和number
是String
,它在内部使用数组。 And an array are indexed from 0 to size-1 . 并且数组从0到size-1进行索引。 Hence such statements will throw ArrayIndexOutOfBoundsException: 因此,此类语句将引发ArrayIndexOutOfBoundsException:
backwards.charAt(5 )
number.charAt(5)
At the time you create your strings, both of your ints are 0, so both of your strings are "0" for the duration of your program. 在创建字符串时,两个int均为0,因此在程序执行期间两个字符串均为“ 0”。 What you really want is the strings to change every time your number changes. 您真正想要的是每次数字更改时字符串都会更改。 So your code should look more like this: 因此,您的代码应更像这样:
public class Digits{
public static void main(String[] args) {
int n = 0;
int b = 0;
String number;
String backwards;
for (int x = 10000; x < 100000 ; x++ ) {
n = x;
b = x *4;
number = Integer.toString(n);
backwards = Integer.toString(b)
. . .
}
In addition, arrays in Java are zero-indexed, so for instance for the string "10000", your program will throw the index out of bounds exception on backwards.charAt(5)
because the string is indexed from character 0 to character 4. 另外,Java中的数组是零索引的,因此,例如对于字符串“ 10000”,您的程序将在backwards.charAt(5)
上使索引超出范围异常,因为该字符串的索引是从字符0到字符4。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.