7 Replies - 864 Views - Last Post: 14 November 2011 - 07:13 AM Rate Topic: ***-- 2 Votes

#1 donsi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 24-November 09

Palindromes

Posted 13 November 2011 - 04:00 PM

I am very confuse in this program. Plz help. it is suppose to accept input from user and then determine if the entered word/phrase is a palindrome or not. I got to this so far. I don't know what els needs to be done. If you guide me in to right directions, i would be much appreciated


import java.io.*;

class palindrome

{

    public static void main(String[] arg) throws IOException


    {

        //Why are you using BufferedReader? Try java.util.Scanner;

        BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));



        //Why are you using improper English in a program?

        System.out.print("Enter a word or phase with no punctuation: ");



        //Variable names should be descriptive

        String UserInput = dataIn.readLine();


       while (UserInput.length() > 0)

        {

			StringBuffer revs = new StringBuffer(UserInput);



                revs = revs.reverse();



                //This should work now that I added a block of code

                if (revs.equals(UserInput))

                {

                    //Only printint out the string?
                    System.out.println("Entered string is a palindrome!");
			  	}

			  	else
			  	{
					System.out.println("Entered string is NOT a palindrome!");
				}


			  	System.out.println("\nEnter a word or phrase with no punctuation: ");
	      UserInput = dataIn.readLine();

		}
	}
}






Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Palindromes

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,237
  • Joined: 27-December 08

Re: Palindromes

Posted 13 November 2011 - 04:14 PM

Please describe your problems or errors specifically. Telling us that it doesn't work isn't descriptive enough for us to help.
Was This Post Helpful? 0
  • +
  • -

#3 donsi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 24-November 09

Re: Palindromes

Posted 13 November 2011 - 04:19 PM

View Postmacosxnerd101, on 13 November 2011 - 04:14 PM, said:

Please describe your problems or errors specifically. Telling us that it doesn't work isn't descriptive enough for us to help.


It complies just fine, but when i run it, it doesn't do anything. "While loop" is running, but mesg doesn't come up saying whether its a palindrome or not.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Palindromes

Posted 13 November 2011 - 04:31 PM

Not sure what StringBuilder.equals() method check
But to be sure I would check

if (revs.toString().equals(UserInput))
Was This Post Helpful? 1
  • +
  • -

#5 donsi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 24-November 09

Re: Palindromes

Posted 14 November 2011 - 05:11 AM

Ok i have fixed it for words, but can't make it work for phrases. plz help



import java.io.*;

class palindrome

{

    public static void main(String[] arg) throws IOException


    {


        BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter a word or phase with no punctuation: ");

        String UserInput = dataIn.readLine();

       while (UserInput.length() > 0)   // while loop

        {

			String str="";
			  int n = UserInput.length();
			  for(int i=n-1;i>=0;i--)
			  str=str+UserInput.charAt(i);   // reversing user input


  				if(str.equals(UserInput))       // checking for palindrome
                  {

                    //Only printint out the string?
                    System.out.println("Entered string is a palindrome!");
			  	}

			  	else
			  	{
					System.out.println("Entered string is NOT a palindrome!");
				}


			  	System.out.print("\nEnter a word or phrase with no punctuation: ");
	      UserInput = dataIn.readLine();

		}
	}
}




Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Palindromes

Posted 14 November 2011 - 06:06 AM

And what do you mean by "but can't make it work for phrases."? Can you elaborate please? What have tried to input and what it gives out?
Was This Post Helpful? 0
  • +
  • -

#7 donsi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 24-November 09

Re: Palindromes

Posted 14 November 2011 - 06:20 AM

View Postsmohd, on 14 November 2011 - 06:06 AM, said:

And what do you mean by "but can't make it work for phrases."? Can you elaborate please? What have tried to input and what it gives out?



If i input "Mom". it says "string is a palindrome",
But if i input "an era came macarena" which is a phrase and also palindrome, but system doesn't recognize it as one. so it mesg comes out " String is NOT a palindrome"

View Postdonsi, on 14 November 2011 - 06:16 AM, said:

View Postsmohd, on 14 November 2011 - 06:06 AM, said:

And what do you mean by "but can't make it work for phrases."? Can you elaborate please? What have tried to input and what it gives out?



If i input "Mom". it says "string is a palindrome",
But if i input "an era came macarena" which is a phrase and also palindrome, but system doesn't recognize it as one. so it mesg comes out " String is NOT a palindrome"


UPDATED CODE

import java.io.*;

class palindrome

{

    public static void main(String[] arg) throws IOException


    {


        BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Playing with Palindromes\n\n");

        System.out.print("Enter a word or phase with no punctuation: ");

        String UserInput = dataIn.readLine();

       while (UserInput.length() > 0)   // while loop

        {


			      String inputUpperCase = UserInput.toUpperCase();

			      UserInput= UserInput.toLowerCase();

			  String str="";
			  UserInput = UserInput.trim();
			  int n = UserInput.length();
			  for(int i=n-1;i>=0;i--)
			  str=str+UserInput.charAt(i);   // reversing user input


  				if(str.equals(UserInput))       // checking for palindrome
                  {

                    // system ouput if word or pharase is palindrome
                    System.out.println("Entered string is a palindrome!\n");
			  	}
					// System output if not palindrome
			  	else
			  	{
					System.out.println("Entered string is NOT a palindrome!\n");
				}


			  	System.out.print("Enter a word or phrase with no punctuation: ");
	      UserInput = dataIn.readLine();

		}
	}
}

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,237
  • Joined: 27-December 08

Re: Palindromes

Posted 14 November 2011 - 07:13 AM

You could use the String replace() method to remove whitespace, and then evaluate the new String.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1