私のコードには 3 つの問題があります。このコードは、キーボードから文字列を読み取り、それをスタックに変換し (小文字を大文字に変換し、その間に空白と句読点を削除します)、2 つを比較し、回文かどうかを判断するように設計されています。
これまでのところ、コード内のエラーを次々と修正してきましたが、現在は 3 つまで減っています (今のところ)。2つは、Stkの5行目で「.\Stk.javaにアクセスできません」と「.\IStk.javaにアクセスできません」ということです(はい、アクセスできることに気づき、突然アクセスできなくなりました)。
3 番目のエラーは、ピリオド記号が行に見つからないことs = infile.nextline();
です。この特定のインスタンスは行 29 にあります。
メインファイルのコードは次のとおりです。
import java.text.*;
import javax.swing.*;
import java.util.*;
import java.util.Scanner;
import java.util.Stack;
public class palindromes {
public static void main(String[] args) {
Scanner infile = new Scanner (System.in);
String s;
int StrLength;
while (s != "Q");
{
// Explains what a palindrome is and asks user to enter test phrase.
System.out.println("This program will test strings to see if they are palindromes.");
System.out.println("A palindrome is a word or phrase that is spelled the same way frontwards and backwards.");
System.out.println("Please enter a string to be tested (Enter 'Q' to quit): ");
// User enters test phrase. If phrase == 'Q', program ends.
s = infile.nextline();
// Converts all letters to caps, removes all white-space and punctuation,
// initializes and stores the value of the length of String 2, initializes the stack.
s.toUpperCase();
s.replaceAll("\\W", "");
s.replaceAll("\\s", "");
StrLength = s.length() - 1;
Stk s2;
s2(s.length());
int i;
// Pushes every letter in the string into the stack.
for (i = 0; i <= StrLength; i++) {
s2.push(s.charAt(i));
};
char c;
i = 0;
// While S3 is not empty, compare popped character with character in same position in string.
// If both are equal, continue until s3 is empty. If there is any difference, end the loop.
while (s2.isEmpty() != TRUE) {
c = s2.pop();
if (c == s.charAt(i)) {
i++;
if (i > StrLenth) {
System.out.println("This string is a palindrome. Please enter another string to test (Enter Q to quit).");
s = infile.nextline();
StrLength = 0;
};
}
else
{
System.out.println("This string is not a palindrome. Please enter another string to test (Enter Q to quit).");
s = infile.nextline();
StrLength = 0;
};
};
};
}
}
以下は、それぞれ Stk.java と IStk.java のコードです。
package palindromes;
import java.io.*;
public class Stk implements IStk {
public Stk ()
{
front=0;
rear=0;
maxStk=0;
items = new char [MAXSIZE];
}
public Stk(int max)
{
maxStk = max+1;
front = maxStk - 1;
rear = maxStk - 1;
items = new char [max];
}
public boolean isFull ()
{
// WRAP AROUND
return ( (top + 1) % maxStk == bot );
}
public boolean isEmpty ()
{
return ( bot == top );
}
public void push (char item)
{
if (!isFull())
{ top = (top+1) % maxStk;
items[top] = item;
}
else
System.out.println ("Tried to insert into full stack.");
}
public char pop ()
{
char item;
if (!isEmpty())
{
item = items[top];
top = (top-1) % maxStk;
}
else
System.out.println ("Tried to remove from empty stack.");
return item;
}
public final int MAXSIZE =100;
private char top;
private char bot;
private int maxStk;
private char items [];
}
public interface IStk
{
/**
* Returns true if Stk is full
* @param none
* @return boolean
*/
public boolean isFull ();
/**
* Returns true if Stk is empty
* @param none
* @return boolean
*/
public boolean isEmpty ();
/**
* Inserts an element at the rear of the Stk
* @param item, the item to be inserted
*/
public void push (char item);
/**
* Returns (and removes) an element from the Stk
* @param item, the item that is removed/returned
*/
public char pop ();
}