gpt4 book ai didi

java - 我不断收到越界错误

转载 作者:行者123 更新时间:2023-12-01 04:49:59 25 4
gpt4 key购买 nike

有人可以帮我确定数组大小吗?反向数组不断越界,我不知道如何修复它。该程序的目的是让用户输入一个句子,程序将找出存在多少个回文,以及它们是什么

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);

}

}

最佳答案

也许你的意思是

reverse[j] = reverse[j] + original.charAt(i);

关于java - 我不断收到越界错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15148383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com