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 14 of 14

Thread: Palindrome

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Palindrome

    Hi I'm am new to Java. How do I take a String argument and return a boolean value?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Palindrome

    public boolean methodName(String param1)
    {
        // some logic/code here
        return true; // or return false;
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    mag12203 (December 18th, 2010)

  4. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Palindrome

    how do I post my code?

  5. #4
    Junior Member
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Palindrome

    Quote Originally Posted by helloworld922 View Post
    public boolean methodName(String param1)
    {
        // some logic/code here
        return true; // or return false;
    }
    Quote Originally Posted by mag12203 View Post
    Hi I'm am new to Java. How do I take a String argument and return a boolean value?
    How do I post my code?

  6. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Cool Palindrome

    import java.util.*;
     
    public class isPalindrome
    {
    static Scanner kb = new Scanner(System.in);
     
    public static void main(String[] args)
    {
     
    System.out.println("Enter words to check if they are a palindrome: (end to file");
     
    while (kb.hasNext() )
    {
     
    String s =kb.nextLine();
    boolean isPal;
     
     
    System.out.println(isPal + s +  "is not a palindrome");
    System.out.println("Enter words to check if they are a palindrome: (end to file");
     
    }
     
    }
     public static boolean isPal(String s)
    {
     
    char ch;
    boolean isPal;
     
    for (int x = 0; x < s.length(); x++)
    {
    ch = s.charAt(x);
    if (Character.isPal(s.charAt(x)))
     
    {
    return true;
    }
    }
    }
    Last edited by helloworld922; December 18th, 2010 at 07:00 PM.

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Palindrome

    In the future, please don't start multiple topics for the same question. I've merged these two together.

    You can post code by using the highlight tags.

    [highlight=Java]
    // your code goes here
    [/highlight]


    This looks like:
    // your code goes here

  8. #7
    Junior Member
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Palindrome

    Ok thnaks Do I put the code in the body of this area surrounded by tags. I do not see a separate place for my code.

  9. #8
    Member
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    32
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Palindrome

    Quote Originally Posted by mag12203 View Post
    Ok thnaks Do I put the code in the body of this area surrounded by tags. I do not see a separate place for my code.
    You paste your code in the body and you write the tags before and after the code.

    You can preview your message to see if it turned out ok before you submit.

    I don't want to sound rude, but you could have gotten all this by reading the welcoming threads. Put yourself in the place of the moderators: they have to answer questions like this every other day. All that time they could have answered sensible questions. So lesson for the future: next time you post something on a forum, please take some time to read the rules threads first.

  10. #9
    Junior Member
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Palindrome

    Quote Originally Posted by mag12203 View Post
    import java.util.*;
     
    public class isPalindrome
    {
    static Scanner kb = new Scanner(System.in);
     
    public static void main(String[] args)
    {
     
    System.out.println("Enter words to check if they are a palindrome: (end to file");
     
    while (kb.hasNext() )
    {
     
    String s =kb.nextLine();
    boolean isPal;
     
     
    System.out.println(isPal + s +  "is not a palindrome");
    System.out.println("Enter words to check if they are a palindrome: (end to file");
     
    }
     
    }
     public static boolean isPal(String s)
    {
     
    char ch;
    boolean isPal;
     
    for (int x = 0; x < s.length(); x++)
    {
    ch = s.charAt(x);
    if (Character.isPal(s.charAt(x)))
     
    {
    return true;
    }
    Is this the correct method for a palindrome. I looked at all the various method under the String class and I don't which one to use. Please help if you can.

  11. #10
    Junior Member
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs down Re: Palindrome

    Quote Originally Posted by Alice View Post
    You paste your code in the body and you write the tags before and after the code.

    You can preview your message to see if it turned out ok before you submit.

    I don't want to sound rude, but you could have gotten all this by reading the welcoming threads. Put yourself in the place of the moderators: they have to answer questions like this every other day. All that time they could have answered sensible questions. So lesson for the future: next time you post something on a forum, please take some time to read the rules threads first.
    Actually I think you rude for first assuming that I did not read the welcoming threads. In fact some of the other members as well respond with rude replys such as calling someone lazy. The moderators have been very respectful but it seems as the members them selves seem to be on some ego trip. People come to this site because most are new to Java and do not want ot feel like we are somehow less intelligent and put in a inferior position because you know more on the subject. Good By Java Forums. I will find another forum IS MORE RESPECTFUL.

  12. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Palindrome

    Quote Originally Posted by mag12203 View Post
    Actually I think you rude for first assuming that I did not read the welcoming threads. In fact some of the other members as well respond with rude replys such as calling someone lazy. The moderators have been very respectful but it seems as the members them selves seem to be on some ego trip. People come to this site because most are new to Java and do not want ot feel like we are somehow less intelligent and put in a inferior position because you know more on the subject. Good By Java Forums. I will find another forum IS MORE RESPECTFUL.
    We must all remember that the contributors, moderators, and admin to the forums do so in their own free time, without pay, all in the effort to help. At times, this help can be mis-interpreted as rude, but it is simply advice. We all must understand that the format of online forums can at times place this advice out of context, resulting in some statements being interpreted in wrong ways, but the original intent of the contributor is to help.

    mag12203, to your problem at hand, the method you posted will not check for a palindrome. You must check each character incrementally in from the beginning and compare that to the character that far in from end of the word to check for equality. So, more along the lines of
    if (s.charAt (locationFromStart) == s.charAt(s.length()-1-locationFromStart))
    Last edited by copeg; December 19th, 2010 at 03:25 PM.

  13. The Following User Says Thank You to copeg For This Useful Post:

    mag12203 (December 20th, 2010)

  14. #12
    Junior Member
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Palindrome

    Quote Originally Posted by copeg View Post
    We must all remember that the contributors, moderators, and admin to the forums do so in their own free time, without pay, all in the effort to help. At times, this help can be mis-interpreted as rude, but it is simply advice. We all must understand that the format of online forums can at times place this advice out of context, resulting in some statements being interpreted in wrong ways, but the original intent of the contributor is to help.

    mag12203, to your problem at hand, the method you posted will not check for a palindrome. You must check each character incrementally in from the beginning and compare that to the character that far in from end of the word to check for equality. So, more along the lines of
    if (s.charAt (locationFromStart) == s.charAt(s.length()-1-locationFromStart))
    Thanks Copeg for your understanding. You might want to check out another Java Forum where the include a section to its member of "how to answer questions in thoughtful way".

  15. #13
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Palindrome

    I presume you are referring to the often quoted 'How to ask Questions the Smart Way', and its subsection:
    How to Answer Questions in a Helpful Way...thanks for mentioning this

  16. #14
    Junior Member
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up Re: Palindrome

    Quote Originally Posted by copeg View Post
    I presume you are referring to the often quoted 'How to ask Questions the Smart Way', and its subsection:
    How to Answer Questions in a Helpful Way...thanks for mentioning this
    Yes that is it.

Similar Threads

  1. Palindrome Stacks
    By mgutierrez19 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 03:05 AM
  2. Check for palindrome numbers
    By SnooSnoo in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 4th, 2010, 06:11 PM