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