简体   繁体   English

我似乎无法修复的编译器错误

[英]Compiler errors I can't seem to fix

I'm having three problems with my code. 我的代码遇到三个问题。 The code is designed to read in a string from the keyboard, convert it to a stack (converting lowercase letters to capitals and deleting whitespace and punctuation while doing so), compare the two, and determine whether or not it is a palindrome. 该代码旨在读取键盘上的字符串,将其转换为堆栈(将小写字母转换为大写字母,并同时删除空格和标点符号),将二者进行比较,然后确定它是否是回文。

Thus far, I have corrected error after error in my code, but now I'm down to three (for the moment). 到目前为止,我已经纠正了我的代码中的一个接一个错误,但现在(现在)我减少到了三个。 Two are that it "cannot access .\\Stk.java" at line and "cannot access .\\IStk.java" at line 5 in Stk (Yes, I noticed it can access it, and then suddenly can't). 两个是在Stk的第5行“不能访问。\\ Stk.java”和在第5行的“不能访问。\\ IStk.java”(是的,我注意到它可以访问,然后突然不能访问)。

The third error is that the period symbol cannot be found in the line s = infile.nextline(); 第三个错误是,在s = infile.nextline();行中s = infile.nextline(); This specific instance is at line 29. 此特定实例在第29行。

Here is the code for the main file. 这是主文件的代码。

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

            };
    };   

}

}

Here is the code from Stk.java and IStk.java respectively. 这是分别来自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 ();
}

I fixed all the compiling errors from all of three classes and some of the most obvious semantic errors (some of them mentioned in the question comments). 我修复了所有三个类的所有编译错误以及一些最明显的语义错误(其中一些在问题注释中提到)。

Compare my version with yours and try to understand my changes. 将我的版本与您的版本进行比较,并尝试了解我的更改。 If you don't get something, leave a question in the comments for this answer. 如果您没有得到任何东西,请在注释中留下有关此答案的问题。

CAUTION: Even though your program is compiling and running, it crashes when calling the push method from Stk! 注意:即使您的程序正在编译和运行,从Stk调用push方法时,它也会崩溃! When I get a bit less busy I'll try to check for the problem but do it yourself too! 当我不那么忙碌时,我将尝试检查问题,但您自己也可以解决!

Yo really should take some time to study more the basics of Java programming, Object Oriented Programming (OOP) and the like. 您确实应该花一些时间来学习更多Java编程,面向对象编程(OOP)等基础知识。 There were a lot of very very basic mistakes in here. 这里有很多非常非常基本的错误。

Here are the files: 这些是文件:

palidromes.java palidromes.java

IStk.java IStk.java

Stk.java Stk.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM