I have been asked to create to check if a word is palindrome or not. But I am facing some problems. After the code you can see the messages that I received from the compiler.

import java.util.*;

public class Palindrome {
	

	public static void main(String[] args)
	{
		[B]Stack<String> stack = new ArrayStack<String>();
		Queue<String> queue = new ArrayQueue<String>();[/B]
		int x = 0;
		
		try
		{
			String word = "kayak";
			
			for(int i = 0; i < word.length(); i++)
			{
				Character temp = word.charAt(i);
				
				[B]stack.push(temp);
				queue.offer(temp);[/B]
			}
			
			while(!queue.isEmpty())
			{
				if(queue.poll() != stack.pop())
				{
					System.out.println("Not a palindrome");
					break;
				}
				else
				{
					
					++x;
					continue;
				}
			}
			
			if(x == word.length())
			{
				System.out.println("It is a palindrome!");
			}
		}
		catch(EmptyStackException e)
		{
			System.out.println("No characters in the stack");
		}
		
			
	}
}

1.ArrayStack cannot be resolved to a type.
2.ArrayQueue cannot be resolved to a type.
3.The method push(String) in the type Stack<String> is not applicable for the arguments (Character).
4.The method offer(String) in the type Queue<String> is not applicable for the arguments (Character).

I appreciate any help from you guys.

Recommended Answers

All 13 Replies

When you post error messages please post the full text of the error message showing the contents of the line and the line number.

Where are ArrayStack and ArrayQueue defined?

For items 3 & 4:
If your Stack takes Strings you can not push other datatypes on it.

For 1 and 2:

Stack<String> stack = new ArrayStack<String>();
Queue<String> queue = new ArrayQueue<String>();

Where are ArrayStack and ArrayQueue defined?

--> Do you mean that I have to create the implementation
Of ArrayStack and ArrayQueue are not inbuild classes of java?

For items 3 & 4:
If your Stack takes Strings you can not push other datatypes on it.

--> So I have to change them to Characters I guess.

I have create my own ArrayStack (actually found it from my lecture notes), but I still get error messages about it in Palindrome class.

Class Palindrome:

public class Palindrome {
	

	public static void main(String[] args)
	{
		Stack<String> stack = new ArrayStack<String>();
		Queue<String> queue = new ArrayQueue<String>();
		int x = 0;
		
		try
		{
			String word = "kayak";
			
			for(int i = 0; i < word.length(); i++)
			{
				Character temp = word.charAt(i);
				
				stack.push(temp);
				queue.offer(temp);
			}
			
			while(!queue.isEmpty())
			{
				if(queue.poll() != stack.pop())
				{
					System.out.println("Not a palindrome");
					break;
				}
				else
				{
					
					++x;
					continue;
				}
			}
			
			if(x == word.length())
			{
				System.out.println("It is a palindrome!");
			}
		}
		catch(EmptyStackException e)
		{
			System.out.println("No characters in the stack");
		}
		
			
	}
}

Class ArrayStack:

public class ArrayStack implements Stack {
    /**
     * Construct the stack.
     */
    public ArrayStack( ) {
        theArray = new Object[ DEFAULT_CAPACITY ];
        topOfStack = -1;
    }
    
    /**
     * Test if the stack is logically empty.
     * @return true if empty, false otherwise.
     */
    public boolean isEmpty( ) {
        return topOfStack == -1;
    }
    
    /**
     * Make the stack logically empty.
     */
    public void makeEmpty( ) {
        topOfStack = -1;
    }
    
    /**
     * Get the most recently inserted item in the stack.
     * Does not alter the stack.
     * @return the most recently inserted item in the stack.
     * @throws UnderflowException if the stack is empty.
     */
    public Object top( ) {
        if( isEmpty( ) )
            throw new UnderflowException( "ArrayStack top" );
        return theArray[ topOfStack ];
    }
    
    /**
     * Remove the most recently inserted item from the stack.
     * @throws UnderflowException if the stack is empty.
     */
    public void pop( ) {
        if( isEmpty( ) )
            throw new UnderflowException( "ArrayStack pop" );
        topOfStack--;
    }
    
    /**
     * Return and remove the most recently inserted item
     * from the stack.
     * @return the most recently inserted item in the stack.
     * @throws Underflow if the stack is empty.
     */
    public Object topAndPop( ) {
        if( isEmpty( ) )
            throw new UnderflowException( "ArrayStack topAndPop" );
        return theArray[ topOfStack-- ];
    }
    
    /**
     * Insert a new item into the stack.
     * @param x the item to insert.
     */
    public void push( Object x ) {
        if( topOfStack + 1 == theArray.length )
            doubleArray( );
        theArray[ ++topOfStack ] = x;
    }
    
    /**
     * Internal method to extend theArray.
     */
    private void doubleArray( ) {
        Object [ ] newArray;
        
        newArray = new Object[ theArray.length * 2 ];
        for( int i = 0; i < theArray.length; i++ )
            newArray[ i ] = theArray[ i ];
        theArray = newArray;
    }
    
    private Object [ ] theArray;
    private int        topOfStack;
    
    private static final int DEFAULT_CAPACITY = 10;
    
    
}

Interface ArrayStack:

public interface Stack {
        /**
         * Insert a new item into the stack.
         * @param x the item to insert.
         */
        void    push( Object x );
        
        /**
         * Remove the most recently inserted item from the stack.
         * @exception UnderflowException if the stack is empty.
         */
        void    pop( );
        
        /**
         * Get the most recently inserted item in the stack.
         * Does not alter the stack.
         * @return the most recently inserted item in the stack.
         * @exception UnderflowException if the stack is empty.
         */
        Object  top( );
        
        
        /**
         * Return and remove the most recently inserted item
         * from the stack.
         * @return the most recently inserted item in the stack.
         * @exception UnderflowException if the stack is empty.
         */
        Object  topAndPop( );
        
        /**
         * Test if the stack is logically empty.
         * @return true if empty, false otherwise.
         */
        boolean isEmpty( );
        
        /**
         * Make the stack logically empty.
         */
        void    makeEmpty( );
    }

Underflow Exception:

public class UnderflowException extends RuntimeException {
    /**
     * Construct this exception object.
     * @param message the error message.
     */
    public UnderflowException( String message ) {
        super( message );
    }
}

The error now is:

In Palindrome class:

Stack<String> stack = new ArrayStack<String>();

--> The Stack does not take any parameters.

Please copy full text of error message and paste it here. Here is a sample:

TestSorts.java:138: cannot find symbol
symbol  : variable var
location: class TestSorts
         var = 2;
         ^

I am sorry Norm but I am using BlueJ and the above message is displayed.

Can you use the javac command in the command prompt to get good error messages for your source?

Why are you using your own classes instead of the java SE classes?

Norm I thought that ArrayStack does not included in Java API. Let me run it with eclipse and I wil post the messages that I will receive

For this code:

import java.util.*;

public class Palindrome {
	

	public static void main(String[] args)
	{
		Stack<String> stack = new ArrayStack<String>();
		Queue<String> queue = new ArrayQueue<String>();
		int x = 0;
		
		try
		{
			String word = "kayak";
			
			for(int i = 0; i < word.length(); i++)
			{
				Character temp = word.charAt(i);
				
				stack.push(temp);
				queue.offer(temp);
			}
			
			while(!queue.isEmpty())
			{
				if(queue.poll() != stack.pop())
				{
					System.out.println("Not a palindrome");
					break;
				}
				else
				{
					
					++x;
					continue;
				}
			}
			
			if(x == word.length())
			{
				System.out.println("It is a palindrome!");
			}
		}
		catch(EmptyStackException e)
		{
			System.out.println("No characters in the stack");
		}
		
			
	}
}

I received this message:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
	ArrayStack cannot be resolved to a type
	ArrayQueue cannot be resolved to a type
	The method push(String) in the type Stack<String> is not applicable for the arguments (Character)
	The method offer(String) in the type Queue<String> is not applicable for the arguments (Character)

	at Palindrome.main(Palindrome.java:9)

2 & 3 -> Where are those two classes defined?
4 & 5 -> wrong data types as arguments to the methods

Did you read the earlier posts? These are EXACTLY the same errors you got before!!!!

Well Norm I have created my own classes:

1)GenericQueue

2)MyStack

3)Palindrome

4)TestPalidrome

Here is my code:

GenericQueue Class:

public class GenericQueue<T>
{
	protected class Node
	{
		T data;
		Node link;
	}

		private Node first;
		private Node last;
		private int count;

	public GenericQueue()
	{
		first = null;
		last = null;
		count = 0;
	}

	public void addToQueue(T value)
	{
		Node add = new Node();
		add.data = value;
		add.link = null;
		if(isEmpty())
		{
			first = add;
			last = add;
		}
		else
		{
			last.link = add;
			last = add;
		}
		count++;
	}

	public T deleteFromQueue()
	{
		T temp = null;

		if(isEmpty())
		{
			System.out.println("Queue is empty cannot delete");
		}
		else
		{
			temp = first.data;
			first = first.link;
			if(first == null)
			{
				last = null;
			}
			count--;
		}
		return temp;
	}

	public boolean isEmpty()
	{
		return count ==0;
	}

	public T look()
	{
		return first.data;
	}

	public int numberOfElements()
	{
		return count;
	}
}

MyStack Class:

import java.util.*;

public class MyStack <T>
{
	private ArrayList<T> stack;

	public MyStack()
	{
		stack = new ArrayList();
	}

	public boolean isEmpty()
	{
		return stack.isEmpty();
	}

	public void push(T x)
	{
		stack.add(x);
	}

	public T pop()
	{
		T temp = null;
		if(isEmpty())
		{
			System.out.println("This Stack is Empty");
		}
		else
		{
			temp = stack.get(stack.size() - 1);
			stack.remove(stack.size() - 1);
		}
		return temp;
	}

	public T look()
	{
		T temp = null;
		if(isEmpty())
		{
			System.out.println("This stack is empty");
		}
		else
		{
			temp = stack.get(stack.size() -1);
		}
		return temp;
	}
}

Palindrome Class:

public class Palindrome
{
	private String sentence;
	private boolean result;
	private MyStack<String> StrStack;
	private GenericQueue<String> StrQueue;

	public Palindrome()
	{
		sentence = null;
		result = false;
		StrStack = new MyStack<String>();
		StrQueue = new GenericQueue<String>();
	}

	public void setSentence(String value)
	{
		sentence = value;
	}

	public void setStackandQueue()
	{
		String[] tokens = sentence.split(" ");
		int index = 0;
		tokens[index] = tokens[index].toLowerCase();
		while(index < tokens.length)
		{
			StrStack.push(tokens[index]);
			StrQueue.addToQueue(tokens[index]);
			index++;
		}
	}

	public void palindromeCompare()
	{
		if(StrQueue.isEmpty() || StrStack.isEmpty())
		{
			result = false;
		}
		else
		{
			while(StrQueue.numberOfElements() != 0)
			{
				if(StrQueue.look().equals(StrStack.look()))
				{
					StrQueue.deleteFromQueue();
					StrStack.pop();
				}
				else
				{
					result = false;
					break;
				}
				result = true;
			}
		}
	}

	public String toString()
	{
		return sentence + "\n" + "is a palindrome sentence? " + result;
	}
}

PalindromeTest class:

import java.util.*;
import java.io.*;
import java.lang.*;

public class PalindromeTest
{
    public static void main(String[]args) throws IOException
    {
        Scanner userInput = new Scanner(new FileReader("Palindrome.txt"));
        Palindrome x = null;
        while(userInput.hasNext())
        {
            x = new Palindrome();
            x.setSentence(userInput.nextLine());
            x.setStackandQueue();
            x.palindromeCompare();
            System.out.println(x);
        }
    }
}

This time I have tried to make my program to read a line to check if is a palindrome or not but I receive the following errors:

java.io.FileNotFoundException: Palindrome.txt (The system cannot find the file specified)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.<init>(FileInputStream.java:106)
	at java.io.FileInputStream.<init>(FileInputStream.java:66)
	at java.io.FileReader.<init>(FileReader.java:41)
	at PalindromeTest.main(PalindromeTest.java:9)
java.io.FileNotFoundException: Palindrome.txt (The system cannot find the file specified)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.<init>(FileInputStream.java:106)
	at java.io.FileInputStream.<init>(FileInputStream.java:66)
	at java.io.FileReader.<init>(FileReader.java:41)
	at PalindromeTest.main(PalindromeTest.java:9)

The system cannot find the file specified

The message says the program can not find the file specified.
You need to put the file in the folder the program is looking in or change the program to look in the folder where the file is.
Or make sure that the spelling of the filename is correct
Or make sure the file exists.

Thanks Norm now is working with Palindrome.txt.txt

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.