Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 118,862 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,705 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Using Stack to Reverse a Palindrome

 
Reply to this topicStart new topic

Using Stack to Reverse a Palindrome, Algorithm

tinyjaymz
post 19 Sep, 2007 - 02:03 PM
Post #1


New D.I.C Head

*
Joined: 19 Sep, 2007
Posts: 7


My Contributions


Here is my code:
CODE

package edu.uhcl.sce.palindrome;
import java.util.Stack;

public class Reverse {  
    
    private Stack<Character> st;
    
    public String reverseString (String s1) {
        StringBuilder sb = new StringBuilder ();
        for (int i=0; i<s1.length(); ++i){
            st.push (s1.charAt(i));
        }
        while (!st.empty()){
            char c = st.pop();
            sb.append(c);
        }
        return sb.toString();
    }        
    public Reverse() {
      
        
    }
    
}


My assignment is to write a simple Java program that uses a java.util.Stack<E> object to test whether a phrase is a palindrome.

We were given two different algorithms to implement into our program, and the previous code was the first one, to be used in the test program.

I'm having trouble converting the second algorithm, which is:

The algorithm for your Main class:

ALGORITHM main()
Get the string s1
Remove all spaces and punctuation from s1
Print s1
Reverse s1
Print the reversed string
if s1.equals(reversed)
Print "is palindrome"
else
Print "is not palindrome"

I know how to do the print statements, using System.out.println(), but the rest, especially the removing spaces, geting the string, and reversing the string, is a bit confusing to me and the book has been no help.

I am using the NetBeans IDE, fyi.

Any help would be great!

Thanks,
Tiny
User is offlineProfile CardPM

Go to the top of the page


tinyjaymz
post 19 Sep, 2007 - 04:15 PM
Post #2


New D.I.C Head

*
Joined: 19 Sep, 2007
Posts: 7


My Contributions


Here is what I have for the 2nd 'Main' algorithm so far...please tell me if I am on the right track.

CODE
package edu.uhcl.sce.palindrome;
import java.util.Scanner;

public class Main {
    
    String s1;
    
    
    public static void main(String[] args) {
        Scanner input = new Scanner ( System.in );
        
    Reverse s1 = new Reverse ();
    
    System.out.print ("Enter a Palindrome: ");
User is offlineProfile CardPM

Go to the top of the page

William_Wilson
post 19 Sep, 2007 - 04:42 PM
Post #3


lost in compilation

Group Icon
Joined: 23 Dec, 2005
Posts: 3,813



Thanked 10 times

Dream Kudos: 3275

Expert In: Java, C, Javascript

My Contributions


i didn't run it, but your reverse method looks good, does it properly reverse a string?
If so simply compare it to the original with the spaces removed as well, if they match voila a palidrome smile.gif

I don't see the part for getting user input, but i imagine you just haven't finished that yet.
User is offlineProfile CardPM

Go to the top of the page

tinyjaymz
post 19 Sep, 2007 - 06:02 PM
Post #4


New D.I.C Head

*
Joined: 19 Sep, 2007
Posts: 7


My Contributions


QUOTE(William_Wilson @ 19 Sep, 2007 - 04:42 PM) *


I don't see the part for getting user input, but i imagine you just haven't finished that yet.



What would be the best way to get input...doing something like s1=next.String...or am I way off?
User is offlineProfile CardPM

Go to the top of the page

William_Wilson
post 19 Sep, 2007 - 06:29 PM
Post #5


lost in compilation

Group Icon
Joined: 23 Dec, 2005
Posts: 3,813



Thanked 10 times

Dream Kudos: 3275

Expert In: Java, C, Javascript

My Contributions


Amadeus (2nd post) gives a good example for an integer, you can adapt this for Strings.
User is offlineProfile CardPM

Go to the top of the page

tinyjaymz
post 19 Sep, 2007 - 10:45 PM
Post #6


New D.I.C Head

*
Joined: 19 Sep, 2007
Posts: 7


My Contributions


Ok, here is my final code:

CODE
package edu.uhcl.sce.palindrome;
import java.util.Stack;
import java.util.Scanner;

public class Main {
    
    
    public static void main(String[ ] args)
   {
    Scanner stdin = new Scanner(System.in);
    String s1;
      
      do
      {
     System.out.println("Enter an expression: ");
         s1 = stdin.nextLine( );
     if (reverseString(s1))
        System.out.println("is palindrome.");
     else
        System.out.println("is not palindrome.");
      }
      while (s1.length( ) != 0);
   }

   public static String reverseString(String input)
   {  
      Stack<Character> st = new Stack<Character>( );
      String s1;
      
      
             StringBuilder sb = new StringBuilder ();
        for (int i=0; i<s1.length(); ++i){
            st.push (s1.charAt(i));
        }
        while (!st.empty()){
            char c = st.pop();
            sb.append(c);
        }
        return sb.toString();
    }

   }


Thanks a bunch for the help...I am having just one more problem:

In the line of code under the first do statement, the line: if (reverseString(s1)) is giving me a problem. In NetBeans, that line has an error that says incompatible types, found java.lang.String, required: boolean This is completely stumping me...can anyone see what i have done wrong?

Thank you.
User is offlineProfile CardPM

Go to the top of the page

William_Wilson
post 20 Sep, 2007 - 05:27 AM
Post #7


lost in compilation

Group Icon
Joined: 23 Dec, 2005
Posts: 3,813



Thanked 10 times

Dream Kudos: 3275

Expert In: Java, C, Javascript

My Contributions


sure, no problem, this seems to be a common issue when learning any language smile.gif

your method declaration:
public static String reverseString(String input)
has a return ^ value ^ of String, but an if statement evaluates booleans, if(true) or if(false).
you are looking for something along the lines of:
if ((reverseString(s1)).equals(s1))
this compares the 2 Strings (properly) and returns a boolean as to whether they are equal.

One other thing, i noticed:
CODE

public static String reverseString(String input)
   {  
      Stack<Character> st = new Stack<Character>( );
      String s1;
      
      
             StringBuilder sb = new StringBuilder ();
        for (int i=0; i<s1.length(); ++i){
            st.push (s1.charAt(i));
        }
        while (!st.empty()){
            char c = st.pop();
            sb.append(c);
        }
        return sb.toString();
    }

   }

you create a variable s1 in this method, but you do not initialize it. It is also determining the for loop's run time, which will by none, since s1.length = 0
I assume this is just a mistake from changing variable names, and you are using it as if it were in the scope of your main method, s1 does nothing in the method, so it should be more like this:
CODE

public static String reverseString(String input)
   {  
      Stack<Character> st = new Stack<Character>( );
    //removed s1 creation
      
      
             StringBuilder sb = new StringBuilder ();
        for (int i=0; i<input.length(); ++i){  //replaced s1 for input
            st.push (input.charAt(i));  //replaced s1 for input
        }
        while (!st.empty()){
            char c = st.pop();
            sb.append(c);
        }
        return sb.toString();
    }

   }

feel free to ask more questions about clarification, or brand new ones smile.gif
User is offlineProfile CardPM

Go to the top of the page

tinyjaymz
post 20 Sep, 2007 - 10:30 AM
Post #8


New D.I.C Head

*
Joined: 19 Sep, 2007
Posts: 7


My Contributions


Thanks, I really appreciate it...it bulids fine. However, apparently I am doing another thing wrong, because it isnt doing exactly what I need it to do. I need it to read the string, print the inputed string (for which i have added a print statement, then remove the punctuation and spaces, make it all caps, and then print the reversed string with no punct., spaces, and in all caps. Do I have the right code and just not initializing it correctly, or do I need to at another method, such as an isLetter function....any suggestions?

Thanks.
User is offlineProfile CardPM

Go to the top of the page

William_Wilson
post 20 Sep, 2007 - 11:46 AM
Post #9


lost in compilation

Group Icon
Joined: 23 Dec, 2005
Posts: 3,813



Thanked 10 times

Dream Kudos: 3275

Expert In: Java, C, Javascript

My Contributions


can you post the complete code you are using?

Have you made sure that the punctuation are being removed properly as well as the switch to capitals?
User is offlineProfile CardPM

Go to the top of the page

tinyjaymz
post 20 Sep, 2007 - 12:02 PM
Post #10


New D.I.C Head

*
Joined: 19 Sep, 2007
Posts: 7


My Contributions


QUOTE(William_Wilson @ 20 Sep, 2007 - 11:46 AM) *

can you post the complete code you are using?

Have you made sure that the punctuation are being removed properly as well as the switch to capitals?



CODE
/*
* Main.java
*
* Created by James Spruiell
*/
package edu.uhcl.sce.palindrome;
import java.util.Stack;
import java.util.Scanner;

public class Main {
    
    
    public static void main(String[ ] args)
   {
    Scanner stdin = new Scanner(System.in);
    String s1;
      
      do
      {
     System.out.println("Enter an expression: ");
         s1 = stdin.nextLine( );
         System.out.println(s1);
     if ((reverseString(s1)).equals(s1))
        System.out.println("is palindrome.");
     else
        System.out.println("is not palindrome.");
        
      }
      while (s1.length( ) != 0);
   }

   public static String reverseString(String s1)
   {  
      Stack<Character> st = new Stack<Character>( );
      
      
             StringBuffer sb = new StringBuffer ();
        for (int i=0; i<s1.length() - 1; ++i){
            st.push (s1.charAt(i));
        }
        while (!st.empty())
        {
            char c = st.pop();
            sb.append(c);
        }
        return sb.toString();
    }
  
}
    


The punctuation isnt being removed, and it isnt being switch to all caps...the only results i get is what I input.

User is offlineProfile CardPM

Go to the top of the page

William_Wilson
post 20 Sep, 2007 - 12:22 PM
Post #11


lost in compilation

Group Icon
Joined: 23 Dec, 2005
Posts: 3,813



Thanked 10 times

Dream Kudos: 3275

Expert In: Java, C, Javascript

My Contributions


to get all capitals:
s1 = s1.toUpperCase();

the punctuation will require a loop or split depending how you go about it, take a shot at it, and i can help you with it.
User is offlineProfile CardPM

Go to the top of the page

tinyjaymz
post 20 Sep, 2007 - 06:51 PM
Post #12


New D.I.C Head

*
Joined: 19 Sep, 2007
Posts: 7


My Contributions


This part is turning out to be hard for me....

I was thinking it would be another for loop, but using "variable name" = s1.charAt(i); instead of the stack...
but have no idea where I would put that..would i create another public static method, or right in the main method.

This post has been edited by tinyjaymz: 20 Sep, 2007 - 06:52 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/13/08 01:51AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month