Results 1 to 6 of 6
  1. #1
    F4iChick02 is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default I need help with my program

    I am trying to write a palindrome program that tests the sentence if it has any uppercase, punctuation, and whitespace. I am just a little confused on how and where to put it into the code. I was hoping somebody could give me suggestions. Thanks.





    import java.util.Scanner;

    public class CheckPalindrome
    {
    public static void main(String[] args)
    {
    Scanner Palindrome = new Scanner(System.in);
    System.out.println("Enter your palindrome input");
    String s = Palindrome.nextLine();


    if (isPalindrome(s))
    System.out.println(s + " is a palindrome");
    else
    System.out.println(s + " is not a palindrome");
    }

    public static boolean isPalindrome(String s)
    {

    String sA = filter(s);
    String sB = reverse(sA);
    return sB.equals(sA);
    }
    public static String filter(String s)
    {
    StringBuffer stringBuffer = new StringBuffer();


    for (int i = 0; i < s.length(); i++)
    {

    if(Character.isLetterOrDigit(s.charAt(i)))
    stringBuffer.append(s.charAt(i));


    }

    return stringBuffer.toString();
    }

    public static String reverse(String s)
    {
    StringBuffer stringBuffer = new StringBuffer(s);
    stringBuffer.reverse();
    return stringBuffer.toString();
    }
    }

  2. #2
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    Part of what it is (I think) is that you don't have your variable as a StringBuffer, sense you're using StringBuffer to make it a reference veriable it needs to be a StringBuffer in Main also and not a String.

    At least, in the one I'm making that is the issue I faced.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Nah, her StringBuffer is fine. She just needs to convert her String parameter, s, to lower case in the filter method, that's all. A simple call to:
    Java Code:
    s = s.toLowerCase();
    as the first line of the filter method might help.

  4. #4
    F4iChick02 is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default About s = s.toLowerCase();

    Can I that to also determine punctuations too? I am sort of confused on how I would do that.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Have you made this change and ran your code yet? I'd do that before asking another question. You may be asking a question that doesn't need asking.

  6. #6
    F4iChick02 is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default Ran it

    Sorry. I just ran it and it worked. I was making it more complicated than it really was. Thank you very much for the help.

Similar Threads

  1. Help with program
    By bri1547 in forum New To Java
    Replies: 16
    Last Post: 07-27-2008, 05:26 AM
  2. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 08:24 AM
  3. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  4. Replies: 0
    Last Post: 10-04-2007, 09:33 PM
  5. Why does this program not end?
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •