CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  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.

  2. #2
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: Java Palindrome Program

    It is supposed to behave wrong on punctuation as your check for "normal" letter is following:
    Code:
    if(input.charAt(i) != ' ')
    so that you add to normalized string all characters but ' ' (and so you add punctuation characters too). You should add only letters.
    "Programs must be written for people to read, and only incidentally for machines to execute."

  3. #3
    Join Date
    Apr 2005
    Posts
    4

    Re: Java Palindrome Program

    Sorry, I'm relatively new at Java. How would I fix that in my code? What do I need to change? Thanks!

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Java Palindrome Program

    Please note that i'm assuming that in Unicode the letters run A-Z uppercase directly followed by a-z lowercase. I may be wrong with this, and if i'm wrong you'll have to alter the boolean accordingly.
    There are many Unicode character sets. You can't rely on a particular character set having a standard English a-z ordering.

    If you want to include only letters (i.e. alphabetic characters), use:
    Code:
    char inChar = input.charAt(i);
    if (Character.isLetter(inChar)) {
        ... // process letter here
    }
    If you want to include letters and digits, the method is:
    Code:
    Character.isLetterOrDigit(inChar)
    See the Character class for more details.

    The limits of your language are the limits of your world...
    L. Wittgenstein.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Apr 2005
    Posts
    4

    Re: Java Palindrome Program

    Ok, now that I have that working, I'm trying to move to the next step. I wan't to write a new program to: read a line from a data file and determine if the entire line is a palindrome. Next, I want to go back and see if each word in the line is a palindrome. How would I use the program I have now to do this.

    Current Palindrome Program:
    import java.util.*;

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

    Scanner scan = new Scanner (System.in);

    //This is the prompt for the 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();

    if(legit(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 true;
    }
    }
    return(true);
    }

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


    result = ""; //It is necessary to initialize result.

    for (int i = 0; i<input.length(); i++)
    {
    char inChar = input.charAt(i);
    if (Character.isLetterOrDigit(inChar))//This is a Java method to determine letters and numbers.
    {
    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;
    }
    }


    Program that returns each word on a new line:

    import java.util.*;

    public class Sentence
    {
    public static void main (String[] args)
    {
    Scanner scan = new Scanner (System.in);

    String input;

    System.out.print ("Please enter a sentence below ");
    input = scan.nextLine();

    StringTokenizer st = new StringTokenizer(input);
    while(st.hasMoreTokens())
    {
    System.out.println(st.nextToken());
    }
    }
    }

    I can do the file input, I just need to figure out how to get the program to address one line at a time, and then each word in a line at a time. Any ideas? Thanks so much for all the help you guys have given me so far!
    Last edited by dataunauthorized; April 20th, 2005 at 06:17 PM. Reason: Saw this post a second too late

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Java Palindrome Program

    To read a text file one line at a time, you can use a BufferedReader wrapped around a FileReader. The FileReader can get characters from a file, and the BufferedReader can return them a line at a time, something like this:
    Code:
    BufferedReader inFile = new BufferedReader(new FileReader("filename.txt"));
    String line = inFile.readLine();
    while(line != null) {
        ... //process line of text
    
        // get next line
        line = inFile.readLine();
    }
    inFile.close();
    Any sufficiently advanced bug is indistiguishable from a feature...
    R. Kulawiec
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

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