Results 1 to 6 of 6
  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Question Palindrome Test Program

    Hello!

    I'm pretty new to Java, however, I've been coding C++ before, and the basics are almost the same there. Now I'm trying to code a program that tests if the name you enter is a palindrome or not, it works perfectly, however, I'm worried if I'm still doing something wrong. Because to compare the letters to each other, I transform them all into capitalized letters, but I don't think I'm doing it right. When I try to print out the individual letters, it works, but when I try to print out the whole name after I've modified it, Java gives me some strange mix of letters and numbers. Could someone take a look at my code and see if I've done something wrong? And if you do find something, could you please propose a better way of doing it?

    Thanks in advance! :)

    //Emil

    Java Code:
    import java.util.Scanner;
    
    public class Grej{
        public static void main(String[] args){
            int counter = 0;
            boolean space = false;
            Scanner hej = new Scanner(System.in);
            char namn[] = new char[30];
            System.out.print("Enter your name: ");
            String data = hej.nextLine();
            namn = data.toCharArray();
            for(int x=0;x<data.length();x++){
                if(namn[x] >= 97 && namn[x] <= 122)
                    namn[x] -= 32;
                else if(namn[x] == ' ')
                    space = true;
            }
            if(space == true)
                System.out.print("Spaces are not allowed! Please try again!");
            else{
                for(int x=0;x<data.length();x++){
                    if(namn[x] == namn[data.length()-1-x]){
                        counter++;
                    }
                }
                if(counter == data.length())
                    System.out.print("Your name is a palindrome! " + namn);
                else
                    System.out.print("Your name is not a palindrome!");
            }
        }
    }
    Last edited by Zelaine; 08-26-2013 at 09:19 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,378
    Rep Power
    5

    Default Re: Palindrome Test Program

    I'm not certain what types of suggestions you are looking for. As long as you have a working program, it is usually inadvisiable to "fix it."

    However, you asked so ....
    Java Code:
    namn = data.toUpperCase().toCharArray();
    Then you just need to make certain the characters are within the desired range.
    You can use character values such as 'A' and 'a' for comparison rather than numerics.

    And as soon as you encounter a space you should display the error and start over. Subequent checking
    won't make the space go away.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    292
    Rep Power
    4

    Default Re: Palindrome Test Program

    Also, what do you want to achieve with this:
    Java Code:
    System.out.print("Your name is a palindrome! " + namn);
    Last edited by milovan; 08-26-2013 at 10:38 PM.

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default Re: Palindrome Test Program

    Determining if a String is a palindrome can be done fairly easy and efficiently if you are comfortable with recursion. Instead of converting the String to a character array, you can work your way inwards and analyze the string 2 characters at a time.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Palindrome Test Program

    <nitpick>
    Java Code:
    if(space == true)
    Is exactly the same as
    Java Code:
    if(space)
    I also highly recommend you use braces around if statements and loops even when they only have one line.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Palindrome Test Program

    Of course the easiest way is to reverse the String and compare to the original.

Similar Threads

  1. Palindrome Program
    By e.cummins95 in forum New To Java
    Replies: 7
    Last Post: 03-09-2012, 12:59 AM
  2. Palindrome program
    By trinity in forum New To Java
    Replies: 4
    Last Post: 04-16-2011, 03:22 AM
  3. Palindrome program help
    By Nel in forum New To Java
    Replies: 1
    Last Post: 03-03-2011, 03:49 AM
  4. Test for Palindrome
    By arson09 in forum New To Java
    Replies: 11
    Last Post: 09-29-2010, 07:57 PM
  5. Palindrome Test
    By Ada in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 01:36 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
  •