Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8076945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:22:10+00:00 2026-06-05T15:22:10+00:00

Full disclosure: this is for an assignment, so please don’t post actual code solutions!

  • 0

Full disclosure: this is for an assignment, so please don’t post actual code solutions!

I have an assignment that requires me to take a string from the user and pass it into a stack and a queue, then use those two to compare the chars to determine if the string is a palindrome. I have the program written, but there appears to be some logic error somewhere. Here’s the relevant code:

public static void main(String[] args) {

    UserInterface ui = new UserInterface();
    Stack stack = new Stack();
    Queue queue = new Queue();
    String cleaned = new String();
    boolean palindrome = true;

    ui.setString("Please give me a palindrome.");
    cleaned = ui.cleanString(ui.getString());

    for (int i = 0; i < cleaned.length(); ++i) {
        stack.push(cleaned.charAt(i));
        queue.enqueue(cleaned.charAt(i));
    }

    while (!stack.isEmpty() && !queue.isEmpty()) {
        if (stack.pop() != queue.dequeue()) {
            palindrome = false;
        }
    }

    if (palindrome) {
        System.out.printf("%s is a palindrome!", ui.getString());
    } else
        System.out.printf("%s is not a palindrome :(", ui.getString());

    stack.dump();
    queue.clear();

}

 public class Stack {

   public void push(char c) {
    c = Character.toUpperCase(c);
    Node oldNode = header;
    header = new Node();
    header.setData(c);
    header.setNext(oldNode);
  }

  public char pop() {
    Node temp = new Node();
    char data;
    if (isEmpty()) {
        System.out.printf("Stack Underflow (pop)\n");
        System.exit(0);
    }
    temp = header;
    data = temp.getData();
    header = header.getNext();
    return data;
  }

}

public class Queue {

  public void enqueue(char c) {
    c = Character.toUpperCase(c);
    Node n = last;
    last = new Node();
    last.setData(c);
    last.setNext(null);
    if (isEmpty()) {
        first = last;
    } else n.setNext(last);     
  }

  public char dequeue() {
    char data;
    data = first.getData();
    first = first.getNext();
    return data;
  }

}

public String cleanString(String s) {
    return s.replaceAll("[^A-Za-z0-9]", "");
}

Basically, when running my code through the debugger in Eclipse, my pop and dequeue methods appear to only select certain alphanumerics. I am using replaceAll("[^A-Za-z0-9]", "") to “clean” the user’s string of any nonalphanumeric chars (!, ?, &, etc.). When I say it only selects certain chars, there doesn’t seem to be any pattern that I can discern. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:22:11+00:00Added an answer on June 5, 2026 at 3:22 pm

    Your general algorithm works properly, assuming your queue and stack are correct (i tried this using the Deque implementations found in the jdk). Since your assignment involves the datastructures, i’ve pretty much just took your main logic and replaced the datastructures with ArrayDequeue, so I don’t feel like i’m answering this for you.

        String word = "ooffoo";
    
        word = word.replaceAll("[^A-Za-z0-9]", "");
    
        Deque<Character> stack = new ArrayDeque<Character>(word.length());
        Deque<Character> queue = new ArrayDeque<Character>(word.length());
    
        for (char c : word.toCharArray()) {
            stack.push(c);
            queue.add(c);
        }
    
        boolean pal = true;
    
        while (! stack.isEmpty() && pal == true) {
            if (! stack.pop().equals(queue.remove())) {
                pal = false;
            }
        }
    
        System.out.println(pal);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, full disclosure: This is going towards a uni assignment, so I don't
Full disclosure : This is for a homework assignment. This is driving me nuts.
First, in order to provide full disclosure, I want to point out that this
Full disclosure: I don't really know Ruby. I'm mostly faking it. I have a
Full disclosure, this is part of a homework assignment (though a small snippet, the
full disclosure - this is for a homework assignment. And I normally would not
Full disclosure...this is my first SO question. Please be kind if I leave anything
Note: Full code can be found at http://github.com/basicxman/andrewhorsman.net I have a Tag model and
Full Disclosure: I am bad at javascript. I'm trying to write something that takes
[Full disclosure: Cross-post between here and ServerFault, because I believe the audiences (server admins

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.