誰かが配列サイズを手伝ってもらえますか?逆配列は範囲外になり続け、私はそれを修正する方法を理解できません。プログラムの目的は、ユーザーに文を入力させることです。プログラムは、存在する回文の数とそれらが何であるかを調べます。
package palindrome;
import java.util.*;
import javax.swing.*;
class Palindrome2
{
public static void main(String args[])
{
//String[] reverse; //Reverse word
String[] words;
int count = 0;
String palindromes[];
//User Input
String original = JOptionPane.showInputDialog("Words that are the same "
+ "forwards and backwards are called palindromes.\n"
+ "This program determines if a word is a palindrome.\n\n"
+ "Enter a word: ");
//Length of the Input
int length = original.length();
//Spliting the original into an Array
words = original.split("\\s");
//Reversing the User's Input
String[] reverse = new String[words.length];
for(int j = 0; j < words.length; j++){
int wordLength = words[j].length();
for ( int i = wordLength - 1 ; i >= 0 ; i-- ) {
reverse[i] = reverse[i] + original.charAt(i);
}
}
//Determining if it is a Palindrome and Output
for (int l = 0; l < words.length; l++){
if (original.equalsIgnoreCase(reverse[l])) {
count = count + 1;
palindromes = new String[count];
palindromes[l] = reverse[l];
}
else {
}
}
JOptionPane.showMessageDialog(null, "There are " + count + " Palindromes"
+ " in this sentence");
JOptionPane.showMessageDialog(null, "The palindromes are:\n"+ palindromes);
}
}