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;
        }
    }
}

Your getCharValue function only tests for lowercase letters(see alphabet variable)
Is that disired?
Normally names start with uppercase letter.
You can use the ToUpper or ToLower methods to correct that.

commented: That was the problem! I actually realized that when I was developing, but I forgot when I came back to the program. Thanks! +0
public void splitData() {
    foreach (string line in lines) {
        parts = line.Split(',');
    }
    Array.Sort(parts);
}

This code is your problem. Each time the loop runs parts is replaced with whatever the current split is so the end result of this code is just the last line split. I suspect you have an empty line at the end of your file so parts has nothing in it.

You'd be better off doing something like

public void splitData() {
    List<string> result = new List<string>();
    foreach (string line in lines) {
        foreach (string part in line.Split(',')) {
            result.Add(part);
        }
    }
    parts = result.ToArray();
    Array.Sort(parts);
}
commented: Nice catch +0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Feb15StingArray
{
    class Program
    {
        char[] alphabet = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
        string[] lines = System.IO.File.ReadAllLines(@"C:\1\test4.txt");
        string[] parts = null;

        static void Main(string[] args)
        {
            Program one = new Program();
            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()
        {
           List<string>list = new List<string>();        
            foreach (string line in lines)
            {
                parts = line.Split(',');
                for (int i = 0; i < parts.Length; i++)
                    list.Add(parts[i]);
            }
            list.Sort();
            parts = null;
            for (int i = 0; i < list.Count; i++)
            {
                Array.Resize(ref parts, i + 1);
                parts[i] = list[i];
            }
        }

        public int getValueForName(string name)
        {
            int totalvalue = 0;
            name = name.Trim();
            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].ToString())
                {
                    charvalue = x + 1;
                    break;
                }
            }
            return charvalue;
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.