Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: The infamous palindrome problem, or i heard so.

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Location
    California, Bay Area
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default The infamous palindrome problem, or i heard so.

    Hello everybody I am a beginner java writer and I have come across a small problem.

    Before I talk about it, let me explain what the actual assignment is.

    ~~~~ASSIGNMENT~~~~
    The file /users/abrick/resources/american-english-insane is a very long list of words, one per line. Open the file and identify the three palindromes more than seven letters long (for the record, they are the names of a Dravidian language, a trademarked farm implement, and a mixture of herbs).

    So to break it down in steps,
    1. Open the file
    2. Test the file for palindromes
    3. Display the palindromes.

    My code:
     
     
     
    /*Programs purpose: Tests files for palindromes that are more than 7 letters long. */
    //Step 1: Open file
    //Step 2: Identify whether or not if the word is a palindrome
    //Step 3: Display palindrome
     
     
     
    import java.util.Scanner;  // Needed for the Scanner Class
    import java.io.*; // Needed for File and IOException
     
    public class assignment8
    {
      public static void main(String []args) throws IOException
      {
     
        // Created a Scanner object for keyboard input.
        Scanner input = new Scanner(System.in);
     
        // Prompts user for file name
        System.out.print("Enter the name of the file to test: ");
        String dictionary  = input.nextLine();
     
        // Opens the file
        File file = new File(dictionary);
        Scanner inputFile = new Scanner(file);
     
            // If there are palindromes, they will be displayed
            if(palindromeCheck(dictionary))
            {
            System.out.println(dictionary+ "is a palindrome");
            }
     
     
      }
            // Checks for palindromes
            public static boolean palindromeCheck(file dictionary)
            {
     
            int low = 0;
            int high = dictionary.length() -1;
     
                    while (low < high)
                    {
                            if (dictionary.charAt(low) != dictionary.charAt(high))
                            {
                            return false;
                            }
                    low++;
                    high--;
                    }
            return true;
            }
     
    }

    So what's the problem with my code?
    well.....
    1. It compiles
    2. It runs fine
    3. But it doesn't print out the palindromes!

    Anybody have any ideas on whats wrong with my code? I need to display the palindromes :\
    work hard, play hard.


  2. #2
    Junior Member
    Join Date
    Sep 2012
    Location
    California, Bay Area
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: The infamous palindrome problem, or i heard so.

    bump, nevermind.... it won't work with file types, only with strings
    work hard, play hard.

Similar Threads

  1. Testing for a Palindrome
    By ryan.sampson in forum Algorithms & Recursion
    Replies: 12
    Last Post: November 6th, 2011, 07:01 PM
  2. Palindrome program help
    By timm1371 in forum Algorithms & Recursion
    Replies: 2
    Last Post: October 13th, 2011, 09:31 AM
  3. Palindrome
    By mag12203 in forum Algorithms & Recursion
    Replies: 13
    Last Post: December 20th, 2010, 07:28 PM
  4. [SOLVED] The infamous Hello World
    By ProfessorBellom in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 17th, 2010, 05:44 PM
  5. the infamous null pointer applet problem
    By wolfgar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 22nd, 2010, 07:09 PM