CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Threaded View

  1. #1
    Join Date
    Apr 2005
    Posts
    4

    Question Next Step In Program

    Hello everyone, I am new to the forum. I have a question on a Java Program I am working on. This program is to recognize only letters and numbers, regardless of case(upper and lower). It is also to ignore punctuation and whitespace so if the user entered the following string: "A man, a plan, a canal, Panama!" the program would print out that the string you entered is a palindrome. Here is what I have so far, and I'm stuck on why it doesn't work when punctuation is included. Could anyone please help me out, I'm shooting to have this ready by tomorrow morning!. Thanks alot for any editing to my code you can provide. Have a good one and thanks again!

    Here's what I have so far:

    import java.util.*;

    public class words
    {
    public static void main (String[] args)
    {
    String input;

    Scanner scan = new Scanner (System.in);

    //promt user to enter a sentence
    System.out.print ("Please enter a sentence below to test if it is a palindrome." +
    "\n\nEnter your sentence here: ");
    input = scan.nextLine();

    input = compress(input);
    input = compress(input);
    String reverse = (checkString(input));
    System.out.println("*"+input+"*"+reverse+"*");
    if(input.equals(reverse))
    {
    System.out.println("The sentence that you entered is a palindrome.");
    }
    else
    {
    System.out.println("The sentence that you entered is not a palindrome.");
    }




    }

    public static boolean legit(String a) //This method returns all numbers and letters.
    {
    for(int i = 0; i < a.length(); i++)
    {
    if(!(Character.isDigit(a.charAt(i)) || Character.isLetter(a.charAt(i))))
    {
    return false;
    }
    }
    return(true);
    }

    public static String compress (String input)//This method eliminates all white space between words.
    {
    String result;


    result = ""; // should not be space at the beginning

    for (int i = 0; i<input.length(); i++)
    {
    if(input.charAt(i) != ' ')
    {
    result = result + input.charAt(i);
    result = result.toLowerCase();
    }
    }
    return result.toLowerCase(); //this is the user entered sentence compressed, in lower case.

    }

    public static String checkString (String last) //this is the method that will reverse the string
    {
    StringBuffer end = new StringBuffer (last);
    end.reverse();
    String palindrome = end.toString();
    return palindrome;
    }
    }
    Last edited by dataunauthorized; April 20th, 2005 at 09:47 PM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured