【发布时间】:2015-10-18 17:20:09
【问题描述】:
我正在测试我的新示例文本:我妈妈是个好厨师。虽然有时在中午左右她会离开,忘记给我做午餐和一些汽水。 @旧作业再次变得重要
我的问题是我没有得到正确的输出,因为我的方法只打印 *Mom a noon i
这都是基于 GUI 的。我正在读取一个文件并检查回文,然后使用堆栈和队列将它们打印到我的 JTextArea 中。
问题是,所有这些都有效,当我启动程序并附加文本文件时,我只得到第一个回文。所以它会打印“妈妈”,这是我的第一个测试用例,但它不会进入它之后的任何其他回文?
我以为我可能在某个时候陷入了代码阻塞的困境,但在修补了一天之后,我现在有点卡住了。
编辑 1:我现在得到更多结果
我的方法是,
public void fileDecode() throws FileNotFoundException
{
if (fileChoice.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
file = fileChoice.getSelectedFile();
scanInput = new Scanner(file);
while(scanInput.hasNext())
{
int nextPalindrome = 0;
int counter = 0;
String token = scanInput.next();
Stack<Character> stk = new Stack<Character>();
Queue<Character> que = new LinkedList<Character>();
for (int i = 0; i < token.length(); ++i)
{
stk.push(token.charAt(i)); //pushing all char's onto the stack/queue
que.add(token.charAt(i));
}
for (int j = 0; j < token.length(); ++j)
{
char tempStk = stk.pop(); //removing them one at a time and checking if they are equal and incrementing counter
char tempQue = que.remove();
if (tempStk == tempQue)
{
counter++;
}
}
if (counter == token.length())
{
build.append(token + " "); //if the counter # was the same as the token's length, than it is indeed a palindrome and we append it into our StringBuilder that we pass to the JTextArea
nextPalindrome = token.length();
}
}
}
}
【问题讨论】:
标签: java algorithm stack queue