Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

need help urgently

Options
  • 01-02-2006 8:30pm
    #1
    Registered Users Posts: 1,298 ✭✭✭


    i have code written for a palindrome, i cant see what the problem is. the code is below

    public class Palindrome


    {

    public static boolean isPalindrome(String theString)

    {
    int index;
    String reverse;

    reverse = "";

    for (index = theString.length() - 1; index >= 0; index--)
    {
    reverse = reverse + theString.charAt(index);
    }



    if(theString.equalsIgnoreCase(reverse))
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    public static void main(String[] args)

    {
    String sentence;
    boolean result;

    System.out.print("Enter a sentence ");
    sentence = EasyIn.getString();
    result = isPalindrome(sentence);

    if (result == true)
    {
    System.out.println(sentence + " is a palindrome");
    }
    else
    {
    System.out.println(sentence + " is not a palindrome ");
    }

    }
    }


Comments

  • Registered Users Posts: 6,494 ✭✭✭daymobrew


    for (index = theString.length(); index >= 0; index--)
    Maybe try
    for (index = theString.length() - 1; index >= 0; index--)
    
    i.e. deduct 1 from theString.length().


  • Closed Accounts Posts: 16,793 ✭✭✭✭Hagar


    I'm not familiar with the language but you should strip all blanks/spaces first. There is no need to check past int(len(string)/2).


  • Registered Users Posts: 5,335 ✭✭✭Cake Fiend


    tom_ass19 wrote:
    for (index = theString.length(); index >= 0; index--)

    I don't know Java, but don't string indexes start at 0?

    In your current code, if I enter a 7-letter word, such as 'example', theString.length() is equal to 7. But theString.charAt(7) would be outside the range of the string, as shown below:
    example
    ^^^^^^^[b]^[/b]
    |||||||[b]|[/b]
    0123456[b]7[/b]
    


  • Closed Accounts Posts: 82 ✭✭cyberbob


    just a guess but you probably shouldnt have that index going right to 0 , hence the out of range error .

    [edit] feck it , got beat [/edit]


  • Registered Users Posts: 1,298 ✭✭✭tom_ass19


    its gettin stuck just after this line:

    sentence = EasyIn.getString();
    result = isPalindrome(sentence);


  • Advertisement
  • Registered Users Posts: 27,087 ✭✭✭✭GreeBo


    what does "getting stuck" mean?


  • Registered Users Posts: 27,087 ✭✭✭✭GreeBo


    cyberbob wrote:
    just a guess but you probably shouldnt have that index going right to 0 , hence the out of range error .

    [edit] feck it , got beat [/edit]
    actually you were wrong, the problem was at the other end of the array :D


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    tom_ass19 wrote:
    its gettin stuck just after this line:

    sentence = EasyIn.getString();
    result = isPalindrome(sentence);

    It's getting stuck in the isPalindrome method, which is called in that line. As everybody else said indexes start at zero and not one.

    <edit>
    Here's a trace tutorial that might help.


  • Registered Users Posts: 1,298 ✭✭✭tom_ass19


    its still not working , i changed it. dont know what to do


  • Registered Users Posts: 5,335 ✭✭✭Cake Fiend


    Post your new code.

    And please use [code] tags to preserve indenting.


  • Advertisement
  • Registered Users Posts: 27,087 ✭✭✭✭GreeBo


    tom_ass19 wrote:
    its still not working , i changed it. dont know what to do
    Have you recompliled it since you changed it?
    (and saved it before you recompile)


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    What's the error now? You could try putting: System.out.println(theSentence); in at the start of the isPalindrome method just to see if the theSentence conatins anything. Or you could just step through the code.

    Or what the GreeBo said :v:


  • Registered Users Posts: 27,087 ✭✭✭✭GreeBo


    Evil Phil wrote:
    Or what the GreeBo said :v:
    Its deffo what GreeBo said because GreeBo just ran it himself :rolleyes:


  • Registered Users Posts: 696 ✭✭✭Magown3


    tom_ass19 wrote:
    int index;

    Initialise index to equal zero.

    i.e. int index = 0;

    when you are looping thru the for loop you are using index as one of the loops guards but you haven't given it a value!
    Not sure if this will fix your problem or not but it's definately good practice.:)


  • Registered Users Posts: 27,087 ✭✭✭✭GreeBo


    Magown3 wrote:
    when you are looping thru the for loop you are using index as one of the loops guards but you haven't given it a value!
    yes he has
    "index = theString.length()"
    however there should be a null check on theString before this stage


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    We're seeing a lot of this palindrome thing lately...


  • Registered Users Posts: 4,188 ✭✭✭pH


    Such a short program and 2 of my pet hates!
    if(theString.equalsIgnoreCase(reverse))
     {
       return true;
     }
    else
     {
       return false;
     }
    }
    

    becomes ....

    return ( theString.equalsIgnoreCase(reverse) );
    if (result == true)
    
    Becomes:

    if (result)

    The 2nd leads to tricky/hard to find WTFs if you mess up and use an assignment (=) rather than equality (==) , the first mainly for brevity and readability.

    This is *not* criticism - the code you wrote works fine, just my pet hates!


  • Closed Accounts Posts: 521 ✭✭✭EOA_Mushy


    tom_ass19 wrote:
    for (index = theString.length() - 1; index >= 0; index--)

    Java yes?
    How did you get it to compile with "length()"? Usualy have to ommit the "()".


  • Registered Users Posts: 27,087 ✭✭✭✭GreeBo


    EOA_Mushy wrote:
    Java yes?
    How did you get it to compile with "length()"? Usualy have to ommit the "()".
    thats for an array, this is a String Object.


  • Registered Users Posts: 27,087 ✭✭✭✭GreeBo


    rsynnott wrote:
    We're seeing a lot of this palindrome thing lately...
    /me is wondering what college has Palindromes on the course at the moment...
    pH wrote:
    Such a short program and 2 of my pet hates!
    True, and he should learn to do it properly in the begining, however this is not something that is usually taught and is an understandable "first effort" from someone.

    Easy to take apart by us, but Im sure my first effort wasnt pretty either.:o


  • Advertisement
  • Registered Users Posts: 27,087 ✭✭✭✭GreeBo


    Hey, just in case you didnt understand what/why there was a problem with you original code here goes my effort at explaning.

    In Java (and most languages) arrays start at 0 and go all they way up to the length of the array -1. So an array of 5 objects has objects 0-4

    So if you put the word cat into an array (in Java)
    you get
    String[] myArray = { "c", "a", "t"};

    which, in our heads looks like [c][a][t]
    so the value in myArray[0] is "c";

    Now while you are not (explicitly) using an array (rather a String object called myString) when you use the charAt method you are accessing the character array that makes up your String because a String in Java is an array of characters (chars).

    Hope that helps...:o


  • Registered Users Posts: 1,298 ✭✭✭tom_ass19


    hey guys, thanks for all the help. i misread a thread earlier, i can go to bed in peace now,haha. Thanks again guys. Ya i saw bout a week back or so something about palindromes. Im finding java hard, i suppose i need to do more work. later guys


  • Closed Accounts Posts: 82 ✭✭cyberbob


    GreeBo wrote:

    Originally Posted by cyberbob
    just a guess but you probably shouldnt have that index going right to 0 , hence the out of range error .

    [edit] feck it , got beat [/edit]


    actually you were wrong, the problem was at the other end of the array :D
    ive never liked zero based arrays


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    cyberbob wrote:
    ive never liked zero based arrays

    Why not? The index indicates displacement. Seems logical enough. Are there any modern languages who use 1-based arrays?


  • Registered Users Posts: 4,188 ✭✭✭pH


    In Pascal you could have any bounds you wanted!

    type array_name = ARRAY [lower..upper] of data_type;
    var myarray : ARRAY [1..100] of integer;


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    Yep, same in FORTRAN. Neither are used much these days.


Advertisement