Results 1 to 5 of 5
  1. #1
    Mika is offline Member
    Join Date
    Dec 2008
    Location
    Davao Oriental
    Posts
    29
    Rep Power
    0

    Exclamation Palindrome problem

    import java.util.Scanner;
    public class Palindrome {
    static Scanner scan=new Scanner(System.in);
    public static void main(String[] args)throws Exception
    {
    char ans='y';
    String one, two;
    int a, index;
    two = "";


    while(ans=='y') {

    System.out.println("Enter a word and check wether it's a palindrome or not: ");

    one = scan.nextLine();
    index = one.length() - 1;
    for(a = 0; a < one.length() ; a++){
    two = two + one.charAt(index - a);

    }

    if(one.equalsIgnoreCase(two)){

    System.out.println("The entered word is a palindrome.");
    }else {
    System.out.println("The entered word is not a palindrome. ");
    }


    System.out.println("Do you want to continue?");
    ans=(char)System.in.read();

    }


    }
    }

    the output of this is wrong:
    Enter a word and check wether it's a palindrome or not:
    madam
    The entered word is a palindrome.
    Do you want to continue?
    y
    Enter a word and check wether it's a palindrome or not:
    The entered word is not a palindrome.
    Do you want to continue?
    Enter a word and check wether it's a palindrome or not:
    The entered word is not a palindrome.
    Do you want to continue?


    The first place, the output is correct but then when the user want to continue it will display this "Enter a word and check wether it's a palindrome or not:
    The entered word is not a palindrome.
    Do you want to continue?" again and again.


    Thank you very much. I need your help pls..

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    please use code tags...

    change ans to type String and use ans=scan.nextLine(); instead of System.in.read();

    also, set two=""; on everyloop.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Try changing the following:
    Java Code:
    one = scan.nextLine();
    to
    Java Code:
    one = scan.next();
    That will fix your problem.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Interesting ....

    You know Mika, I just have reviewed your post history for this year and you never have said thanks for the help you have received. That's not very nice for somebody who has come continiously to the forum for help.

    Just a thought...
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Another way to do this

    Just thought I would throw in my 2 cents worth...
    The method that the OP is using is basically taking the original word, flipping it backwards and comparing it with the original word. This is Ok and acceptable. Another way to do this would be to compare the opposite ends of the letters in the word. For example...
    word -> abccba
    place -> 123456

    compare letter 1 vs letter 6, then
    compare letter 2 vs letter 5, then
    compare letter 3 vs letter 4
    The advantage of this is performance. A non-palidrome word would be detected a lot faster than taking the original word, flipping it and then comparing the two words.

    Just a though....

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. StringTokenizer in a Palindrome program
    By jeremyk in forum New To Java
    Replies: 10
    Last Post: 02-13-2010, 06:35 PM
  2. 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
  •