[英]java String - String index out of range , charAt
I try to make a program which it can find palindromic number (it has to be pruduct of two 3-digits number and I hope that it contain 6 digit but it is not important). 我尝试制作一个可以找到回文数的程序(它必须是两个3位数字的产品,我希望它包含6位数字,但这并不重要)。 Here is my code: 这是我的代码:
public class palindromicNumber {
public static void getPalindromicNumber() {
boolean podminka = false;
int test;
String s;
for (int a = 999; podminka == false && a > 100; a--) {
for (int b = 999; podminka == false && b > 100; b--) {
test = a * b;
s = Integer.toString(test);
int c = 0;
int d = s.length();
while (c != d && podminka == false) {
if (s.charAt(c) == s.charAt(d)) { // I think that problem is here but I can't see what
System.out.println(s);
podminka = true;
}
c++;
d--;
}
}
}
}
}
and if I want to compile it : 如果我想编译它:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.charAt(String.java:695)
at faktorizace.palindromicNumber.getPalindromicNumber(palindromicNumber.java:24)
at faktorizace.Faktorizace.main(Faktorizace.java:19)
Java Result: 1 Java结果:1
There are two problems here: 这里有两个问题:
If c
starts off odd and d
starts off even, then c
will never equal d
. 如果c
从奇数开始,而d
从偶数开始,则c
永远不会等于d
。 You need to use 您需要使用
while (c < d && !podminka) // Prefer !x to x == false
Additionally, judicious use of break
and return
would avoid you having to have podminka
at all. 此外,明智地使用break
和return
将避免您完全拥有podminka
。
As another aside, you've got a separation of concerns issue. 另外 ,您还有一个关注点分离问题。 Your method currently does three things: 您的方法当前执行三件事:
You should separate those out. 您应该将它们分开。 For example: 例如:
public void printFirstPalindrome() {
long palindrome = findFirstPalindrome();
System.out.println(palindrome);
}
public long findFirstPalindrome() {
// Looping here, calling isPalindrome
}
public boolean isPalindrome(long value) {
// Just checking here
}
I suspect findFirstPalindrome
would normally take some parameters, too. 我怀疑findFirstPalindrome
通常也会采用一些参数。 At this point, you'd have methods which would be somewhat easier to both write and test. 在这一点上,您将拥有一些在编写和测试方面都比较容易的方法。
String indices go from [0..length - 1]
字符串索引从[0..length - 1]
Change int d = s.length();
更改int d = s.length();
to int d = s.length() - 1;
到int d = s.length() - 1;
Update : As a quick aside, you are setting podminka
to true
when 更新 : podminka
,您将podminka
设置为true
时
s.charAt(c) == s.charAt(d)
If s = 100101
for example, you will terminate all of the loops on the first iteration of the while loop because the first and last characters are the same. 例如,如果s = 100101
,则您将在while循环的第一次迭代中终止所有循环,因为第一个和最后一个字符相同。
int d = s.length();
An array of the strings chars will only go from 0 - length-1. 字符串chars的数组只能从0-length-1开始。
s.charAt(d)
will always be out of bounds on the first iteration. s.charAt(d)
在第一次迭代中将始终超出范围。
Take a look on JDK source code: 看一下JDK源代码:
public char charAt(int index) {
if ((index < 0) || (index >= count)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index + offset];
}
You can see that this exception is thrown when index is less then zero or exceeds the string length. 您可以看到,当index小于零或超过字符串长度时,将引发此异常。 Now use debugger, debug your code and see why do you pass this wrong parameter value to charAt()
. 现在使用调试器,调试代码,然后查看为什么将此错误的参数值传递给charAt()
。
public class palindromicNumber {
public static void getPalindromicNumber(){
boolean podminka = false;
int test;
String s;
for(int a = 999;podminka == false && a>100; a-- ){
for(int b = 999;podminka == false && b>100; b-- ){
test = a*b;
s = Integer.toString(test);
int c = 0;
int d = s.length();
while(c!=d && podminka == false){
if(s.charAt(c)==s.charAt(d - 1)){
System.out.println(s);
podminka = true;
}
c++;
d--;
}
}
} }
try this! 尝试这个! string count starts from 0! 字符串数从0开始!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.