我想检查是否一个字符串,输入的字符串,该字符串是回文检查回文(JAVA)(新秀状态)
,如果它是显示一些积极 如果不.. (无效)
我目前得到的答案无效(无论输入什么内容) 我不太确定如果for循环或布尔语句有问题。
//ACTION FROM BUTTON PERFORMED HERE
private void ButtonActionPerformed(ActionEvent evt) {
//creating variables
String myString = textField1.getText();
int stringLength = myString.length();
char arrayOne[] = new char[stringLength];
char arrayTwo[] = new char[stringLength];
boolean palindrome = false;
//for loop to setup ARRAY ONE
for(int i = 0; i < stringLength-1; i++){
arrayOne[i] = myString.charAt(i);
}
//for loop to setup ARRAY TWO
for(int i = stringLength-1; stringLength-1 > i; i--){
arrayTwo[i] = myString.charAt(i);
}
//for loop checking if array indexes are equivalent in value (char)
for(int i = 0; i < stringLength-1; i++){
if(arrayOne[i] != arrayTwo[i]){
palindrome = false;
}
else{
palindrome = true;
}
}
//assigning text to the text boxes based on boolean palindrome
if(palindrome == true){
textField2.setText("Valid");
}
if(palindrome ==false){
textField2.setText("Invalid");
}
}
}
我想我评论它descently
可以使用调试器或添加调试代码。 – 2013-02-22 06:17:09
http://stackoverflow.com/questions/4138827/check-string-for-palindrome?rq=1 – 2013-02-22 06:29:22