So for some bizzare reason instead of getting any values I get 0 if I try
using a loop to get values using the method getValueForName. However if I just call it from main and pass it a string as an arg it gives me value.
I'm new to C#, and I have no clue what I could be doing wrong. Help appreciated.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace ProjectEuler
{
class Problem22
{
string[] alphabet = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Marek\Desktop\names.txt");
string[] parts = null;
static void Main(string[] args)
{
Problem22 one = new Problem22();
one.splitData();
one.run(one.parts);
Console.WriteLine(one.getValueForName("s"));
Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
public void run(string[]array)
{
foreach (string line in array)
{
Console.WriteLine(getValueForName(line));
}
}
public void splitData()
{
foreach (string line in lines)
{
parts = line.Split(',');
}
Array.Sort(parts);
}
public int getValueForName(string name)
{
int totalvalue = 0;
char[]namechar = name.ToCharArray();
foreach(char c in namechar)
{
totalvalue += getCharValue(c);
}
return totalvalue;
}
public int getCharValue(char character)
{
int charvalue = 0;
for (int x = 0; x< alphabet.Length; x++)
{
if (character.ToString() == alphabet[x])
{
charvalue = x + 1;
break;
}
}
return charvalue;
}
}
}