4

I'm working on an assignment for my programming class but I've run into some difficulty and I'm not sure where else to look. Basically the question asks that we write a program that checks for palindromes.

  • The user enters text (No non-alphanumberic chars allowed.)
  • The String is pushed one character at a time into a stack
  • The characters are pulled one at a time out of the stack thus reversing the String
  • If the original is the same as the reverse, we have a palindrome

I'm having some trouble with my loops though and don't know where to go from here, does anyone have any advice or pointers? What am I doing wrong?

Here's what I have so far.

import java.util.Stack;
import java.util.regex.*;
import javax.swing.*;

public class Question1 {

    static Stack PDrome = new Stack();

    public static String Reverse (String input) {
        String reverse;

        if (input.length() <= 1) {
            return input;
        }   

        //pushing onto the stack
        for (int i=0; i<input.length();i++) {
            PDrome.push(input.charAt(i));
        }


        //popping from the stack into the string
        for (int i=0; i<input.length(); i++) {  
            PDrome.pop()=reverse.charAt(i);
        }  

        return reverse;
    }

    //Illegal char check method
    public static boolean checker (String input) {
        Pattern p = Pattern.compile("[^a-z0-9]", Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(input);
        boolean b = m.find();

        if (b) {
            System.out.println("There is a special character in your string");   
            System.exit(0);
        }   

        return b;    
    }


    //Main
    public static void main (String [] args) {
        //input
        String input = JOptionPane.showInputDialog("Enter text to check if it's a palndrome");

        //error case
        if (input==null); {
            System.out.println("Nothing Entered");
            System.exit(0);
        }

        //checking for illegal chars
        checker(input);
    }
}
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Eogcloud
  • 1,335
  • 4
  • 19
  • 44

4 Answers4

3

This part:

String reverse;
...
//popping from the stack into the string
for (int i=0; i<input.length(); i++)
{   
    PDrome.pop()=reverse.charAt(i);
}  

should be like this:

String reverse = "";
...
//popping from the stack into the string
for (int i=0; i<input.length(); i++)
{   
    // appends the popped character to reverse
    reverse += PDrome.pop();
}  

note that when appending a large number of string, this isn't the best way to do it since Java's string is immutable and repeatedly appending a string would require creating a new string each time. This problem is small enough that it wouldn't really matter though, but when the problem gets large you'll want to use StringBuffer/StringBuilder.

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
  • +1: for the `StringBuffer` advice. One thing: StringBuffer is synchronized. Since Java 5 there's `StringBuilder`. It's the same but not syncrhonized so it's better for one-threaded needs, like using it inside a method as a local variable (most of the time). – helios Jan 25 '12 at 15:14
2

PDrome.pop()=reverse.charAt(i); is wrong.

  1. reverse is null -> NullPointerException
  2. Are you assigning a value to a function? (pop())

You have to build reverse from pop'ping from the stack.

So you should start with an empty string: reverse = ""; and add the chars taken from the stack:

while (!PDrome.isEmpty())
   reverse += PDrome.pop();

Naming detail

Please use non capital letters to start fields and method names:

"someIntegerVariable"
"methodForCalculation"

and only capital letters to start class and interface names:

Stack
ArrayList
MyClass

:)

(from the Java conventions)

helios
  • 13,574
  • 2
  • 45
  • 55
  • Ahhhhh! I see what I was doing wrong, made sense in my head but I couldn't see how it was wrong. Also, thanks for the top on methods – Eogcloud Jan 25 '12 at 14:47
  • 1
    @Eogcloud - Consider using StringBuilder instead of concat'ing the chars - get used to good practices already :) (String-concat will result in a new string-object being allocated upon every concat!) – quaylar Jan 25 '12 at 15:04
2

What are you actually doing here?

PDrome.pop()=reverse.charAt(i);

You should have use PDrome.pop() to retrieve one char at a time and append it to reverse.

Sufian Latif
  • 13,086
  • 3
  • 33
  • 70
  • Yeah it was almost more pseudocode than anything to try and get my idea across, I see how I'm wrong though! – Eogcloud Jan 25 '12 at 14:50
1

This is a much cleaner way to write it in my opinion. It is a recursive approach.

bool isPalindrome(String s)
{
    if(s.length() <= 1)
        return true;

    return s[0] == s[s.length() - 1] && isPalindrome(s.substr(1, s.length() - 2);
}

It is MUCH shorter as you can see.

theDazzler
  • 1,039
  • 2
  • 11
  • 27