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

Palindromes

Options
  • 25-01-2006 4:32pm
    #1
    Registered Users Posts: 1,298 ✭✭✭


    Heya. Im just having some problems. Im trying to make up code for a palindrome sentence in Java. I know the basics. I just need a kind of a kick start to get me going. Does anyone have any information and could help me with my work.

    Regards,
    Xavior


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Show us what you have done.


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    As I've read it, you give your program a sentence and it determines if its a palindrome or not. Is this what your on about???


  • Registered Users Posts: 1,606 ✭✭✭djmarkus


    Im trying to make up code for a palindrome sentence in Java

    Be more specific, are you trying to detect if a sentence entered is a palindrome?


  • Registered Users Posts: 15,944 ✭✭✭✭Villain


    God I remember doing that in pascal, many moons ago, it just a matter of stepping through the characters using two loops one that starts from the first letter and the other that starts from the last letter?

    Or maybe I'm wrong?


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


    silas wrote:
    As I've read it, you give your program a sentence and it determines if its a palindrome or not. Is this what your on about???

    ya thats basically it. a very basic program. see i have all notes written out on paper but not in jcreator yet. but i know that there wrong. I just need a kick start is all.

    basically all the program has to do is be able to see that the word "stats" is a palindrome and that the word "apple" isn't.


  • Advertisement
  • Registered Users Posts: 20,915 ✭✭✭✭Stark


    xavior99 wrote:
    see i have all notes written out on paper but not in jcreator yet.

    Can we see them?

    ⛥ ̸̱̼̞͛̀̓̈́͘#C̶̼̭͕̎̿͝R̶̦̮̜̃̓͌O̶̬͙̓͝W̸̜̥͈̐̾͐Ṋ̵̲͔̫̽̎̚͠ͅT̸͓͒͐H̵͔͠È̶̖̳̘͍͓̂W̴̢̋̈͒͛̋I̶͕͑͠T̵̻͈̜͂̇Č̵̤̟̑̾̂̽H̸̰̺̏̓ ̴̜̗̝̱̹͛́̊̒͝⛥



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


    public class Palindrome
    {

    // Student Name :
    // Student Id Number :
    // Date :
    // Purpose : To show if a sentence or word is or is not a Palindrome

    public static void main(String[] args)

    {

    String sentence;


    System.out.print("Enter a sentence ");
    sentence = EasyIn.getString();
    System.out.println();
    if (firstLast(sentence) ++ true);


    {
    System.out.println("This is a Palindrome");
    }
    else
    {
    System.out.println("This is not a Palindrome");
    }

    }

    }


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


    public class Palindrome
    {

    // Student Name :
    // Student Id Number :
    // Date :
    // Purpose : To show if a sentence or word is or is not a Palindrome

    public static void main(String[] args)

    {

    String sentence;


    System.out.print("Enter a sentence ");
    sentence = EasyIn.getString();
    System.out.println();
    if (firstLast(sentence) ++ true);


    {
    System.out.println("This is a Palindrome");
    }
    else
    {
    System.out.println("This is not a Palindrome");
    }

    }

    }




    im terrible at programming, i know that is all wrong. i dont know wat to do. dont laugh at my stupid stupid mistakes plz...


  • Registered Users Posts: 20,915 ✭✭✭✭Stark


    You're nowhere close. Start with pseudocode (to save yourself the nitty gritty programming details). Write down the steps you would do in your head if someone wrote a word in front of you and asked if it was a palindrome. Then work on converting these basic steps into actual code.

    ⛥ ̸̱̼̞͛̀̓̈́͘#C̶̼̭͕̎̿͝R̶̦̮̜̃̓͌O̶̬͙̓͝W̸̜̥͈̐̾͐Ṋ̵̲͔̫̽̎̚͠ͅT̸͓͒͐H̵͔͠È̶̖̳̘͍͓̂W̴̢̋̈͒͛̋I̶͕͑͠T̵̻͈̜͂̇Č̵̤̟̑̾̂̽H̸̰̺̏̓ ̴̜̗̝̱̹͛́̊̒͝⛥



  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    irish1 wrote:
    God I remember doing that in pascal, many moons ago, it just a matter of stepping through the characters using two loops one that starts from the first letter and the other that starts from the last letter?
    Damn I remember doing a similar project in pascal back in 1st year, hated that fecking language.

    OP, thats perfectly straighforward, so ya got a plan so try it out, JCreator won't be long telling you if your wrong.


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


    see thats the thing, my head wont go any further, its like i have a mental blocl or something...its driving me crazy ya no.


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    public class Palindrome
    {
    
    // Student Name :
    // Student Id Number :
    // Date :
    // Purpose : To show if a sentence or word is or is not a Palindrome
    
    public static void main(String[] args)
    
    {
    
    String sentence;
    
    
    System.out.print("Enter a sentence ");
    sentence = EasyIn.getString();
    System.out.println();
    if (firstLast(sentence) ++ true);
    
    
    {
    System.out.println("This is a Palindrome");
    }
    else
    {
    System.out.println("This is not a Palindrome");
    }
    
    }
    
    }
    
    Okay where's firstlast?

    You need to loop through the sentence string.
    for ( int i = 0 ; i < sentence.length ( ) ; i++ )
    {
    // sentence[i] is the current character 
    }
    


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


    sorry man, where does that go in. do i have to get rid of something from the code and replace it wit this:

    for ( int i = 0 ; i < sentence.length ( ) ; i++ )
    {
    // sentence is the current character
    }


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


    forgot to mention, im gettin one error where my else statment is. the error is:

    "else" without "if"


  • Registered Users Posts: 20,915 ✭✭✭✭Stark


    xavior99 wrote:
    see thats the thing, my head wont go any further, its like i have a mental blocl or something...its driving me crazy ya no.

    If you get to drop out now and pay only half fees for the year, then I suggest you do so. Being able to put your thoughts in writing, even if it's not code, is the fundamental skill you need to learn in order to program. You can look at a word and tell if it's a palindrome or not, the crucial skill is observing what goes on in your mind during that decision process. Write down anything at all, chances are once it's anyway coherent, it can be converted into code.

    agwxtkjasjrwwhhwwrjsajktxwga

    Tell me if the above "word" was a palindrome, then explain how you knew it was a palindrome.

    ⛥ ̸̱̼̞͛̀̓̈́͘#C̶̼̭͕̎̿͝R̶̦̮̜̃̓͌O̶̬͙̓͝W̸̜̥͈̐̾͐Ṋ̵̲͔̫̽̎̚͠ͅT̸͓͒͐H̵͔͠È̶̖̳̘͍͓̂W̴̢̋̈͒͛̋I̶͕͑͠T̵̻͈̜͂̇Č̵̤̟̑̾̂̽H̸̰̺̏̓ ̴̜̗̝̱̹͛́̊̒͝⛥



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


    In a word, yep.
    There is no point in us handing you the solution or telling you where to put in chunks of code if you have no idea what it is you are trying to do.

    I reckon you are too caught up in the "code" side of things, it happens so dont drop out just yet.

    As has been said, how do you know that abba is a palindrome but that abbab is not?
    Write down the steps you are performing in your head, in fact, say out loud the steps you are performing and write that down, in english.

    Then turn that into pseudo code
    i.e figure out if some repeatable step you are taking should be a look
    if some check you are doing should be a conditional statement (an "if")

    Post what you have and we will help, but a handout at this stage will only hurt you longterm.


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


    i know guys, i dont want to drop out, im getting high 80's n in the 90's in my other subjects. but im failing programming, its driving me crazy.

    ive written some new code. its all messed around, dont know where it should all go. im back home, was in college earlier, this is just straight from my notes. i didnt test it at all on jcreator


    public static void main(String[] args)

    String new = "navan";
    int counter = 0;
    while ( counter < new.length() );

    {
    if (new.charAt(counter) == new.charAt (new.length(counter)) )

    boolean Palindrome = true;
    boolean Palindrome = false;

    }
    //end if

    counter ++
    //end while

    System.out.print(Palendrome);



    this is just all bits n pieces, i know it dosent make any sense but does it look like i could do something better with this than my last attempt


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Stop writing code. Its not a code problem yet, its still a procedure one. Answer the question:
    Stark wrote:
    agwxtkjasjrwwhhwwrjsajktxwga

    Tell me if the above "word" was a palindrome, then explain how you knew it was a palindrome.


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


    sorry aiden. yes it is a palindrome. well it seems to be, its not a trick palindrome is it coz i only went over it quickly. just like:

    evil olive

    thats a palindrome rite.


  • Closed Accounts Posts: 521 ✭✭✭EOA_Mushy


    xavior99 wrote:
    sorry aiden. yes it is a palindrome. well it seems to be... coz i only went over it quickly.

    Dude, you looking for help, put some efford into it.
    xavior99 wrote:
    just like:

    evil olive

    thats a palindrome rite.

    No includeing the space, yes excluding......

    Your going to love it when they ask you to do the same useing recursion later. :)


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


    If you're just looking for how to do it what's wrong with typing palindrome and java into google and using any of the 76,000 results?

    I like ...

    satan oscillate my metallic sonatas


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Write down in steps how you would deduce that: agwxtkjasjrwwhhwwrjsajktxwga is a palindrome.

    How did you tell? What did you compare to what? Remember what the definition of a palindrome is, its a word thats spelt the same forwards as well as backwards... *cough hint cough*

    EDIT: If you're just going to google and copy/paste the answer, or even "read it for inspiration" then you'll probably fail computers, especially when it gets hard. If you can't write down in english how you'd go about answering this problem (ignoring the code side) you really need to put some serious effort into your computers and get someone to tell you how to think (i'm not joking btw, coding is all about thinking. Everyone can think, but not everyone thinks in the right way. You need to be able to think in logical steps and write down each and every step along the way from start to finish, its the only way to code imo).


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


    yeah, quit writing code.
    Its just confusing you and stopping you from thinking logically, which is what you need to do.
    You know that your code example makes no sense so why bother?
    Write out in English the steps you need to use to discover if *ANY* sentence is a palindrome.
    Until then you are not going to get the answer you want here.


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


    Just to mention that a normal definition of Palindrome would ignore punctuation and capitals when comparing - hence :

    A man, a plan, a canal; Panama.

    Including its spaces and punctuation IS a palindrome.


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


    EOA_Mushy wrote:

    Your going to love it when they ask you to do the same useing recursion later. :)

    And then there's Prolog...
    pH wrote:
    satan oscillate my metallic sonatas

    Ah, yes, Stephen Fry. Sadly, already more obscure than when first uttered; the sonatas in question were on cassette tape, hence metallic.


  • Closed Accounts Posts: 521 ✭✭✭EOA_Mushy


    pH wrote:
    Just to mention that a normal definition of Palindrome would ignore punctuation and capitals when comparing - hence :

    A man, a plan, a canal; Panama.

    Including its spaces and punctuation IS a palindrome.

    My bad... :)


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    xavior99 wrote:
    ive written some new code. ....

    this is just all bits n pieces, i know it dosent make any sense but does it look like i could do something better with this than my last attempt

    If you can't explain what your code is doing then you probably didn't write it. Can you write out exactly what you think your code is doing.

    As mentioned write out in steps how you determine what a palindrome is and then for each step look up how the code would do the same step. If you can't do that then you may as well quit the class.


  • Moderators, Category Moderators, Motoring & Transport Moderators Posts: 21,238 CMod ✭✭✭✭Eoin


    Write down in steps how you would deduce that: agwxtkjasjrwwhhwwrjsajktxwga is a palindrome.

    How did you tell? What did you compare to what? Remember what the definition of a palindrome is, its a word thats spelt the same forwards as well as backwards... *cough hint cough*

    The above advice should be enough to get you going, but in case it isn't (taking the punctuation issue out of it for the time being):

    Take a word like "hannah". How would you tell it's a palindrome?

    The 1st letter should equal the last "h == h"
    The 2nd letter should equal the 2nd last "a == a"
    The 3rd letter should equal the 3rd last "n == n"

    NB: The following is in C# not pure Java, but there shouldn't be too many differences in the syntax.

    To get the 1st letter:
    string sWord = "hannah";
    string sFirstChar = sWord.Substring(0,1);
    

    The last letter is going to be the length of the word minus 1
    string sWord = "hannah";
    int nWordLen = sWord.Length;
    string sFirstChar = sWord.Substring(0,1);
    string sLastChar = sWord.Substring(nWordLen-1,1);
    

    To get the 2nd and 2nd last letters, it would be:
    string sSecondChar = sWord.Substring(1,1);
    string sSecondLastChar = sWord.Substring(nWordLen-2,1);
    

    Now, all you have to do is compare them:
    if (!sFirstChar.Equals(sLastChar))
    {
        // not a palindrome	
    }
    

    Obviously the next thing to do is take the concept above, and put it into a loop without referencing individual characters. I don't particularly want to include the full script together, as you should have enough info from all the posts on this thread to piece it together.


  • Closed Accounts Posts: 140 ✭✭Sneaky_Russian


    for a simple one excluding the punctuation....

    i am thinking.

    split in 2,
    reverse 1,
    and compare the 2

    PS i only skimmed over the posts, sorry in advance....


  • Advertisement
Advertisement