tags:

views:

393

answers:

3

I am trying to create a console application that reads number from a file all underneath each other like so:

101
9

and then outputs into another file the closest palindrome number. So far what I have is not quite right; i.e. I don't think I can put the class inside a method which is a bit more Java I was wondering if anyone could help at all?

namespace PalidromeC
{
    class Program
    {
        static public void Main(string[] args)
        {
            try
            {
                StreamWriter sw = new StreamWriter("C://Temp/PalindromeAnswers.txt");
                sw.WriteLine("Answers");
                sw.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }

            try
            {
                string numbers;

                StreamReader sr = new StreamReader("C://Temp/Palindrome.txt");
                numbers = sr.ReadLine();

                while (numbers != null)
                {
                    Console.WriteLine(numbers);
                    numbers = sr.ReadLine();
                }

                sr.Close();
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }

            NearPalindromeFinder f = new NearPalindromeFinder();
            int palindrome = f.findNearPalindrome(n);
            Console.WriteLine("Nearest Palindrome = " + palindrome);
        } // Main()

        public static void testFindNearestPalindrome(int n)
        {
            class NearPalindromeFinder 
            {
                int findNearPalindrome(int start) 
                {
                    if (testPalindrome(start))
                        return start;
                    else 
                    {
                        int neg = start;
                        int pos = start;

                        for (int i = 0; i < start; i++) 
                        {
                            if (testPalindrome(start-i)) 
                            {
                                neg = start-i;
                                break;
                            }

                            if (testPalindrome(start+i)) 
                            {
                                pos = start+i;
                                break;
                            }
                        }

                        return (start == neg) ? pos : neg;
                     }
                 }

                 bool testPalindrome(int start) 
                 {
                     if (start == 0 || start == 1)
                         return true;

                     String str = String.valueOf(start);
                     String rev = new        
                     if (str.equals(rev))
                         return true;
                     else
                         return false;
                 }
            } // class NearPalindromeFinder 
        } // findNearPalindrome()
    } // class Program
} // namespace
+1  A: 

No you can't embed a class in a function, and you shouldn't have to. Just get rid of your testFindNearestPalindrome function and move the NearPalindromeFinder class to outside of your Program class.

However, your NearPalindromeFinder class doesn't have any state - it just needs to expose the findNearPalindrome method, so you'd be better off making it a public static function, and making the testPalindrome method private:


namespace PalidromeC
{
    class Program
    {
        static public void Main(string[] args)
        {    
          ...        

          int palindrome = NearPalindromeFinder .findNearPalindrome(n);
          Console.WriteLine("Nearest Palindrome = " + palindrome);

        }//main
    }

    class NearPalindromeFinder 
    {
        public static int findNearPalindrome(int start) 
        {
           if (testPalindrome(start))
             return start;
           else 
           {
             int neg = start;
             int pos = start;
             for (int i = 0; i < start; i++) 
             {
               if (testPalindrome(start-i)) 
               {
                 neg = start-i;
                 break;
               }//if
               if (testPalindrome(start+i)) 
               {
                 pos = start+i;
                 break;
               }//if                                                       
             }//for
             return (start == neg) ? pos : neg;
           }//else
         }//findNearPalindrome


         private static bool testPalindrome(int start) 
         {
           if (start == 0 || start == 1)
             return true;
           String str = String.valueOf(start);
           String rev = // TODO: Reverse str here       
           if (str.equals(rev))
             return true;
           else
             return false;
         }//testPalindrome

    }//NearPalindromeFinder class

}//namespace
Mark Pim
Thank you so much :)
No worries. :-)
Mark Pim
+1  A: 

Eliminate the testFindNearestPalindrome method, and declare int findNearPalindrome(int start) as public int findNearPalindrome(int start), which should get your code to compile.

dsolimano
A: 

you can't put a class inside a function re work your layout like so.

namespace PalidromeC
{
    class Program
    {
        static public void Main(string[] args)
        {
            ...
            NearPalindromeFinder finder = new NearPalindromeFinder();
            finder.findNearPalindrome(...);
        }
    }
    class NearPalindromeFinder 
    {
        int findNearPalindrome(int start) 
        {
        ...
        }
        bool testPalindrome(int start) 
        {
        ...
        }

}
Hath