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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

help needed with palindrome

  • 05-05-2006 2:25pm
    #1
    Closed Accounts Posts: 578 ✭✭✭


    hi,
    sorry to bother everyone, but i need to hand up this make-up assignment as im failing my CA. the deadline is tomorrow. we recently handed up this assignment ( palindrome) my program reads from the command line (written in java) it does everything the lectuer stated for me to do, but she still failed me and wanted me to do this extra work on it. if i dont get this done i fail software design. as far as i can see with my program it does everything she has asked me to do.

    here is what she wants

    f you add the following to your Assignment it should bring the mark up.

    1. Command Line Arguments - So that the user can enter a filename from
    the Command Line (see attached file). At the moment your Assignment asks
    the user to enter a filename.
    2. When checking if the String is a Palindrome or not you should remove
    spaces, e.g. a man a plan a canal panama is only a palindrome if the
    spaces are removed.
    4. Tidy up the code, and comment it properly.
    5. Add more methods, e.g. A method to count the words etc.

    here is the code
    import java.io.*;
    import java.util.*;
    
    class Assign{
    
        public static void main(String args[])
        {
                   
            
            String entry = new String();
          
           // lets file be read in through command line
                     entry = args[0];
                    
            int lower = 0;
            int palindromeChecker = 0;
            int noOfWords = 0;
    
                    // reads file in (pract 13)
                      try
            {
                                    FileReader fileReader = new FileReader(entry);
                                   
                                    Scanner inputFile = new Scanner(fileReader);
    
               
      
                String fromFile = inputFile.nextLine();
              
                int higher = fromFile.length() - 1;
              
                              // checks for spaces to determine words
                for(int i = 0; i < fromFile.length(); i++)
                {
                    char spaceChecker = fromFile.charAt(i);
                    if(spaceChecker == ' ')
                        noOfWords++;
                }
                if(noOfWords > 0)
                    System.out.println ("The string in this file is " + (noOfWords + 1) + " words long.");
                else
                    System.out.println("The string in this file is only one word long.");
                  
                // checks if the word is a palindrome or not (Pract 9)  
                while (lower<higher)
                {
                    if (fromFile.charAt(lower) != fromFile.charAt(higher))
                        palindromeChecker++;
                      
                    lower++;
                    higher--;
                }
                if(palindromeChecker != 0)
                    System.out.println("The string you entered was not a palindrome");
                else
                    System.out.println("The string you entered was a palindrome");
    
          
                fileReader.close();
                }
                                    // displays if there is an exception
                    catch (IOException e)
                    {
                            System.err.println ("Unable to read from file");
                            System.exit(-1);
                    }
    
        }
    }
    


Comments

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


    I think your going to fail.


  • Closed Accounts Posts: 578 ✭✭✭inode


    well thanks very much for that


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


    Well what she asked for is very basic. If you actually wrote that code then doing the remaining portion would be a doddle.


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


    1. Take in the string (a)
    2. Create a new string (b) which is the first string with the spaces stripped out.
    3. Create a blank string (c)
    4. Step backwards through string (b) 1 character at a time and append each character to string (c)
    5. String (c) is now string (b) reversed character by character.
    6. Check to see if (b) and (c) are indentical.
    7. If they are you have a palindrome if they are not you do not have a plaindrome.

    Add the comments at each stage stating the purpose of the stage.
    Display a result.

    Hope that helps, that's a simple as I can make it without writing code for you.


  • Closed Accounts Posts: 578 ✭✭✭inode


    your right, i will get this done later tonight and post up my conclusion.

    thanks very much for that


  • Advertisement
  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    homework. banned. etc.


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


    Hagar wrote:
    1. Take in the string (a)
    2. Create a new string (b) which is the first string with the spaces stripped out.
    3. Create a blank string (c)
    4. Step backwards through string (b) 1 character at a time and append each character to string (c)
    5. String (c) is now string (b) reversed character by character.
    6. Check to see if (b) and (c) are indentical.
    7. If they are you have a palindrome if they are not you do not have a plaindrome.

    Add the comments at each stage stating the purpose of the stage.
    Display a result.

    Hope that helps, that's a simple as I can make it without writing code for you.

    Except if you read the code it is already doing the palindrome checking, so what you have suggested has absolutly nothing to do with what he has been told to add to the code.


  • Registered Users, Registered Users 2 Posts: 5,112 ✭✭✭Blowfish


    Hobbes wrote:
    I think your going to fail.
    ROFL! Sad but true.


This discussion has been closed.
Advertisement