Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: need help telling if a word or phrase ins a palindrome in java

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help telling if a word or phrase ins a palindrome in java

    my code works but only for words...How could i make my code so it can also tell if phrases (such as taco cat) are palindromes.
    package Homework5;
    import java.util.Scanner;
     
    class HW5
    {
       public static void main(String args[])  {
          String original, reverse = "";
          Scanner in = new Scanner(System.in);
     
          System.out.println("Enter a word or phrase and Iíll tell you if itís a palindrome.");
          original = in.nextLine();
     
          int length = original.length();
     
          for ( int i = length - 1; i >= 0; i-- )
          {
             reverse = reverse + original.charAt(i);
          }
     
          if (original.equals(reverse))
          {
             System.out.println(original+ " is a palindrome!");
          }
          else
          {
             System.out.println(original+ " is not a palindrome!");
          }
     
       }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: need help telling if a word or phrase ins a palindrome in java

    Throw out spaces?

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: need help telling if a word or phrase ins a palindrome in java

    add in an if statement, saying if charAt(i)==' ' (a space) then move to the next one. Basically, just ignore spaces.

  4. #4
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help telling if a word or phrase ins a palindrome in java

    so what exactly would i put inside the if statement?
    i know it would be
    if(original.charAt(i)==" ")
    {
    (What would go here?)
    }
    Last edited by HappyCamper; October 5th, 2014 at 01:32 PM.

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: need help telling if a word or phrase ins a palindrome in java

    then do nothing.

  6. #6
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help telling if a word or phrase ins a palindrome in java

    how would i write that...just leave an empty space?

  7. #7
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: need help telling if a word or phrase ins a palindrome in java

    you would just leave it empty...

     
    if(space)
    do nothing
    else
    do the normal code

Similar Threads

  1. Java 1.6.0_21 x 64 - Windows 7, Add-ins
    By DAVESINGER in forum Java Theory & Questions
    Replies: 3
    Last Post: July 24th, 2014, 07:11 AM
  2. Java 1.6.0_21 x 64 - Windows 7, Add-ins
    By DAVESINGER in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 24th, 2014, 05:41 AM
  3. Replies: 3
    Last Post: June 27th, 2013, 07:27 AM
  4. Replies: 7
    Last Post: March 29th, 2013, 07:38 AM
  5. stack Palindrome , java
    By Faha in forum Object Oriented Programming
    Replies: 1
    Last Post: May 3rd, 2011, 04:20 PM