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

Thread: Testing for a Palindrome

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Testing for a Palindrome

    So I have this very simple program, it is designed to be a simple text editor. You type in a text, and you have a few options, including add words and replace words etc.. the one thing that i cannot get to work is if you type "pal", the program will say back if its a palindrome or not. i took out the rest of the program to pretty up my post. my two problems are where "pal" method is executed is incorrect, and the character.isletter statements but here is what i have:

    package Assignment6;
    import java.util.Scanner;

    public class part2 {
    public static void main(String[] args) {
    System.out.print("Enter the text to be edited: ");
    Scanner console = new Scanner(System.in); //Creates console scanner

    String text = console.nextLine(); // input string Text (initial text)

    System.out.print("Edit: ");
    String command = console.nextLine(); // Command string input
    String function = command.substring(0,3).toLowerCase(); //takes first three letters and makes "function"
    String pars = command.substring(3).trim(); //creates "pars" as the rest of the string, this is the parameters

    //works up to insert

    while (!function.equals("sto")){ //creates while and creates stop point (sto)
    if (function.equals("ins")){ //if it equals insert
    text = insert(text, pars); // use text to insert parameters goes to INSERT METHOD
    System.out.println("The changed text is: " + text);} //prints the updated text
    else if (function.equals("rep")){ //if it equals replace
    text = replaceFirst(text,pars);
    System.out.println("The changed text is: " + text);
    }
    else if (function.equals("del")) {
    text = delete(text, pars);
    System.out.println("The changed text is: " + text);

    } else if (function.equals("pal")){
    text = palindrome(String text);

    }
    else {
    System.out.println("The function " + function + " is not defined. Original text is not changed.");
    System.out.println("The unchanged text is: " + text);
    }

    System.out.print("Edit: ");
    command = console.nextLine();
    function = command.substring(0,3).toLowerCase();
    pars = command.substring(3).trim();


    }


    System.out.println("Program stopped");
    }
    public static void palindrome(String text){
    int left, right;
    char charLeft, charRight;
    text.toLowerCase();
    {
    left = 0;
    right = text.length() - 1;
    while (left < right){
    charLeft = text.charAt(left);
    charRight = text.charAt(right);
    if (charLeft == charRight){
    left++;
    right--;}
    if Character.isLetter(charLeft){
    left++;}
    if Character.isLetter(charRight){
    right--;}
    else break;
    }
    System.out.println();
    if (left < right)
    System.out.println ("This text is not a palindrome: " + text);
    else
    System.out.println ("This text is a palindrome: " + text);
    return;
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Testing for a Palindrome

    where "pal" method is executed is incorrect,
    Please show what your code does and explain what is incorrect.

    Please edit your post, select the code and wrap it in code tags. Use the Go Advanced button and then the #icon.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Testing for a Palindrome

    I'm not sure how to use the advanced.. but here goes..

    so this is the part that isn't correct:
    } else if (function.equals("pal")){
    text = palindrome(String text);

    the other part is this:
    if (charLeft == charRight){
    left++;
    right--;}
    if Character.isLetter(charLeft){
    left++;}
    if Character.isLetter(charRight){
    right--;}
    else break;
    }

    what i want it to do is to skip any periods, commas, spaces, etc so that it is only comparing characters. so if my text input is "madam, i'm adam" it will return saying "this is a palindrome"

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Testing for a Palindrome

    String str = "hello";
    System.out.println(String str);

    Surely you can see what is wrong in the above code.
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Testing for a Palindrome

    should it be:

    String str = "hello";
    System.out.println(str);

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Testing for a Palindrome

    I'm not sure how to use the advanced.
    There is a button below the input box with a label "Go Advanced" >>>>>>>>>>>>>> press it.
    his is the part that isn't correct:
    What is the code doing now? what are the values of charLeft and charRight as the code is executed?
    What value is returned by the isLetter() methods?

    Do some debugging by adding printlns to your code to see what the values for the above are.
    Print them out so you can see what your code is seeing.

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Testing for a Palindrome

    i've been working with the debugger for hours now..
    i've changed it a bit but here is what i'm getting for a response:

    Enter the text to be edited: atoyota
    Edit: pal
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	Syntax error on token "if", new expected
    	Character.isLetter cannot be resolved to a type
    	Syntax error, insert "}" to complete ClassBody
    	Syntax error, insert ";" to complete Statement
    	Syntax error on token "if", new expected
    	Syntax error on token "--", invalid VariableDeclarator
    	Syntax error on token "else", ; expected
    	Syntax error on token "}", delete this token
     
    	at Assignment6.part2.palindrome(part2.java:119)
    	at Assignment6.part2.main(part2.java:38)

    for some reason it doesn't like my if statement? i'm sure this is just how i'm writing it, not so much that its completely wrong

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Testing for a Palindrome

    Where is the if statement that the compiler is having problems with?

  9. The Following User Says Thank You to Norm For This Useful Post:

    ryan.sampson (November 6th, 2011)

  10. #9
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Testing for a Palindrome

    ok i'm moving forward... i got it to run all the way through.. but it now thinks that everything is a palindrone.. leading me to believe that it is skipping parts.. here is the updated code:

    package Assignment6;
    import java.util.Scanner;
     
    /**
     * Name: Ryan Sampson
     * Course: COS 160 TuTh 4:10-5:30
     * Project: String Processing
     * Filename: part2.java
     * 	 */
     
    public class part2 {
        public static void main(String[] args) {
            System.out.print("Enter the text to be edited: ");
            Scanner console = new Scanner(System.in); //Creates console scanner
     
            String text = console.nextLine();  // input string Text (initial text)
     
            System.out.print("Edit: "); 
            String command = console.nextLine(); // Command string input
            String function = command.substring(0,3).toLowerCase(); //takes first three letters and makes "function"
            String pars = command.substring(3).trim(); //creates "pars" as the rest of the string, this is the parameters
     
            //works up to insert
     
            while (!function.equals("sto")){ //creates while and creates stop point (sto)
               if (function.equals("ins")){ //if it equals insert
                    text = insert(text, pars); // use text to insert parameters goes to INSERT METHOD
                    System.out.println("The changed text is: " + text);} //prints the updated text
               else if (function.equals("rep")){ //if it equals replace
            	   text = replaceFirst(text,pars);
            	   System.out.println("The changed text is: " + text);  
               }
               else if (function.equals("del")) {
            	   text = delete(text, pars);
            	   System.out.println("The changed text is: " + text);
     
               } else if (function.equals("pal")){
            	   text = palindrome(text);   
               }
               else {
            	   System.out.println("The function " + function + " is not defined. Original text is not changed.");
            	   System.out.println("The unchanged text is: " + text);
               }
     
                System.out.print("Edit: ");
                command = console.nextLine();
                function = command.substring(0,3).toLowerCase();
                pars = command.substring(3).trim();
     
     
            }
     
     
            System.out.println("Program stopped");
        }  
     
        public static String insert(String text, String parameters){
     
            String first = getString(parameters);
            parameters = parameters.replaceFirst("\"" + first + "\"","");
            int index = Integer.parseInt(parameters.trim());
     
            text = text.substring(0, index) + first + text.substring(index);
            return text;
        }
     
        public static String replaceFirst(String text, String parameters){
     
        	String first = getString(parameters);
        	parameters = parameters.substring(parameters.indexOf(" "), parameters.length()).trim();
        	String second = getString(parameters);
     
        	text = text.replaceFirst(first,second);
     
        	return text;
     
     
        }
     
        public static String delete(String text, String parameters){
        	String firstNumber = parameters.substring(0, parameters.indexOf(" "));
            int firstindex = Integer.parseInt(firstNumber);
            parameters = parameters.substring(parameters.indexOf(" "), parameters.length()).trim();
            int secondindex = Integer.parseInt(parameters.trim());
     
            String subString1 = text.substring(0,firstindex);
            String subString2 = text.substring(firstindex, text.length());
            String subString3 = text.substring(firstindex, secondindex + 1);
            subString2 = subString2.replaceFirst(subString3, " ");
            text = subString1 + subString2;
            return text;
     
        }
     
        public static String getString(String parameters){
     
            int nextQuote = parameters.indexOf('"');
            parameters = parameters.substring(nextQuote + 1);
            nextQuote = parameters.indexOf('"');
            String returnString = parameters.substring(0, nextQuote);
            return returnString;
        }
     
     
        public static String palindrome(String text){
            int left, right;
            char charLeft, charRight;
            text.toLowerCase();
            {
                left = 0;
                right = text.length() - 1;
                while (left < right){
                    charLeft = text.charAt(left);
                    charRight = text.charAt(right);
                    if (charLeft == charRight){
                            left++;
                            right--;}
                   	else Character.isLetter(charLeft);{
                            left++;
                    Character.isLetter(charRight);
                            right--;}
                }
            System.out.println();
                if (left < right)
                        System.out.println ("This text is not a palindrome: " + text);
                                        else
                        System.out.println ("This text is a palindrome: " + text);
                return text;
            }
         }            
        }

    If i use text "madam, i'm adam" and run pal, it says its a palindrome.. which is correct. but it says that for everything.. not sure whats going on?? and i appreciate the help a ton guys!! thanks

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Testing for a Palindrome

    You need to debug your code. Add some printlns to it so you can see the values that the program is seeing and using to make decisions. Print out the values of all the variables that it is using, especially left, right, charLeft and charRight.

  12. #11
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Testing for a Palindrome

    so using the debugger... its going thru the letters correctly, the problem is it gets the left and it gets the right, but even though left == right is there, and if they aren't the same, it keeps going .. do i have something in the wrong order?

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Testing for a Palindrome

    do i have something in the wrong order?
    That question should be answered by your debugging your code. Look at all the values and see which ones your code is not handling correctly.

    Use a piece of paper to write down what the values should be.
    Then compare them with the values the program sees when it is executed.

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Testing for a Palindrome

    Your usage of {}s is terrible. You need to align the ending } vertically in the same column underneath the beginning of the statement with the starting {

    Also useful is to add comments on the } that end loops and methods.

Similar Threads

  1. Palindrome program help
    By timm1371 in forum Algorithms & Recursion
    Replies: 2
    Last Post: October 13th, 2011, 10:31 AM
  2. stack Palindrome , java
    By Faha in forum Object Oriented Programming
    Replies: 1
    Last Post: May 3rd, 2011, 05:20 PM
  3. Palindrome
    By mag12203 in forum Algorithms & Recursion
    Replies: 13
    Last Post: December 20th, 2010, 08:28 PM
  4. Palindrome Stacks
    By mgutierrez19 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 04:05 AM
  5. Check for palindrome numbers
    By SnooSnoo in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 4th, 2010, 06:11 PM