Trey Brumley
Trey Brumley

Reputation: 25

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

The third error is that the period symbol cannot be found in the line s = infile.nextline(); This specific instance is at line 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.

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

Upvotes: 0

Views: 96

Answers (1)

DallaRosa
DallaRosa

Reputation: 5825

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! 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. There were a lot of very very basic mistakes in here.

Here are the files:

palidromes.java

IStk.java

Stk.java

Upvotes: 1

Related Questions