I cannot get my array to convert to int. I tried some ways but always ran into errors. Here is my program as it stands now. Can anyone give me some pointers???

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;

namespace sortingarray
{
    class Program
    {
        static void Main(string[] args)
        {
            String phoneareacode, state, line;
            AreaCode myAreaCode;
            bool isNum = false;

            try
            {
                string[] strings = File.ReadAllLines("C:/CS280files/AreaCodes.csv");
                string[][] unsortedarray = new string[strings.Length][];
                for (int i = 0; i < strings.Length; i++)
                {
                    unsortedarray[i] = strings[i].Split(',');
                }
            
              Array.Sort(unsortedarray, AreaCode.sortAreaCode());

                for (int i = 0; i < unsortedarray.Length; i++)
                {
                    phoneareacode = unsortedarray[i][0];
                    state = unsortedarray[i][1];
                   
                    myAreaCode = new AreaCode(phoneareacode, state);   
                }

                int searchAreaCode;
                string searchString = Console.ReadLine();
                isNum = Int32.TryParse(searchString, out searchAreaCode);
              
                    if (isNum)
                    {
                        int lowNum = 0;
                        int highNum = unsortedarray.Length - 1;

                        while (lowNum <= highNum)
                        {
                            int midNum = (lowNum + highNum) / 2;
                            if (searchAreaCode < unsortedarray[midNum])
                            {
                                highNum = midNum - 1;
                            }
                            else if (searchAreaCode > unsortedarray[midNum])
                            {
                                lowNum = midNum + 1;
                            }
                            else if (searchAreaCode = unsortedarray[midNum])
                            {
                                Console.WriteLine(" The state for Area Code {0} is: ", searchAreaCode);
                                return;
                            }
                        }
                               
                        Console.WriteLine("Please enter a 3 digit area code!");
                    //Console.WriteLine(myAreaCode);
                    //Console.WriteLine();
                    //Console.ReadKey(true);
                }                      
            }
            catch (IOException e)
            {
                Console.WriteLine(e);
            }
        }
    }
}

This is the errors I am getting:

Visual Studio 2008\Projects\sortingarray\sortingarray\Program.cs(49,33): error CS0019: Operator '<' cannot be applied to operands of type 'int' and 'string[]'
Visual Studio 2008\Projects\sortingarray\sortingarray\Program.cs(53,38): error CS0019: Operator '>' cannot be applied to operands of type 'int' and 'string[]'
Visual Studio 2008\Projects\sortingarray\sortingarray\Program.cs(57,55): error CS0029: Cannot implicitly convert type 'string[]' to 'int'

The 2 errors have the same cause. You are trying to compare a string with an int. You can compare strings with strings or ints with ints. The last error tells you, you are trying to assign a string to an int.BTW on line 57: you cannot use assign(=) here, you must use equal(==)
You could compare with searchstring, not with SearchAreaCode.

Edited 7 Years Ago by ddanbe: n/a

The 2 errors have the same cause. You are trying to compare a string with an int. You can compare strings with strings or ints with ints. The last error tells you, you are trying to assign a string to an int.BTW on line 57: you cannot use assign(=) here, you must use equal(==)
You could compare with searchstring, not with SearchAreaCode.

I thought you were completely right, but after putting in those corrections, I get the same errors.

Please show us your updated code. Just from a statement that it is still wrong we can infer little...

Yea, you are right. Here is the updated code:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;

namespace sortingarray
{
    class Program
    {
        static void Main(string[] args)
        {
            String phoneareacode, state, line;
            AreaCode myAreaCode;
            bool isNum = false;

            try
            {
                string[] strings = File.ReadAllLines("C:/CS280files/AreaCodes.csv");
                string[][] unsortedarray = new string[strings.Length][];
                for (int i = 0; i < strings.Length; i++)
                {
                    unsortedarray[i] = strings[i].Split(',');
                }
            
              Array.Sort(unsortedarray, AreaCode.sortAreaCode());

                for (int i = 0; i < unsortedarray.Length; i++)
                {
                    phoneareacode = unsortedarray[i][0];
                    state = unsortedarray[i][1];
                   
                    myAreaCode = new AreaCode(phoneareacode, state);   
                }

                int searchAreaCode;
                string searchString = Console.ReadLine();
                isNum = Int32.TryParse(searchString, out searchAreaCode);
              
                    if (isNum)
                    {
                        int lowNum = 0;
                        int highNum = unsortedarray.Length - 1;

                        while (lowNum <= highNum)
                        {
                            int midNum = (lowNum + highNum) / 2;
                            if (searchString < unsortedarray[midNum])
                            {
                                highNum = midNum - 1;
                            }
                            else if (searchString > unsortedarray[midNum])
                            {
                                lowNum = midNum + 1;
                            }
                            else if (searchAreaCode == unsortedarray[midNum])
                            {
                                Console.WriteLine(" The state for Area Code {0} is: ", searchAreaCode);
                                return;
                            }
                        }
                               
                        Console.WriteLine("Please enter a 3 digit area code!");
                    //Console.WriteLine(myAreaCode);
                    //Console.WriteLine();
                    //Console.ReadKey(true);
                }                      
            }
            catch (IOException e)
            {
                Console.WriteLine(e);
            }
        }
    }
}

The errrors:
Visual Studio 2008\Projects\sortingarray\sortingarray\Program.cs(49,33): error CS0019: Operator '<' cannot be applied to operands of type 'string' and 'string[]'
Visual Studio 2008\Projects\sortingarray\sortingarray\Program.cs(53,38): error CS0019: Operator '>' cannot be applied to operands of type 'string' and 'string[]'
Visual Studio 2008\Projects\sortingarray\sortingarray\Program.cs(57,38): error CS0019: Operator '==' cannot be applied to operands of type 'int' and 'string[]'

Your unsortedarray is two dimensions, but you are comparing it as though it were only one. Look at line 31 and see how you accessing areacode, then look at your if statement at line 49--see the difference?

So I should have for line 49 something like unsortedarray[][]?

edit: I can see that will not work either.

Edited 7 Years Ago by mejohnm: n/a

I think you need to read up on using multi-dimensional arrays in C# so you understand what you are doing, but in both of those brackets you need numbers to access the string instead of a string[], which is what it currently resolves to.

Not to discourage you from learning how to access multi-dimensional arrays, but you should consider using a strongly typed List<> instead, if you instructor would approve.

Type List<> is the very next question. In fact, I got the rest of this assignment done just not this one. This one, I need to read in the file that has area code with state abbreviation. Sort it and then do a binary search to find what the user inputs. I did it using Array.BinarySearch but that is not what the professor wants. I will go back and reread about multi-dimensional arrays since I am so stucked here.

ok, I went back to the basics. I got the sorting done and even the binary search comes up correct. Now I just need the index to show the actual state. Here is the code and it has no errors:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;

namespace sortingarray
{
    class Program
    {
        static void Main(string[] args)
        {
            String phoneareacode, state, line;
            AreaCode myAreaCode;
            bool isNum = false;

            try
            {
                string[] strings = File.ReadAllLines("C:/CS280files/AreaCodes.csv");
                string[][] unsortedarray = new string[strings.Length][];
                for (int i = 0; i < strings.Length; i++)
                {
                    unsortedarray[i] = strings[i].Split(',');
                }

                Array.Sort(unsortedarray, AreaCode.sortAreaCode());

                for (int i = 0; i < unsortedarray.Length; i++)
                {
                    phoneareacode = unsortedarray[i][0];
                    state = unsortedarray[i][1];

                    myAreaCode = new AreaCode(phoneareacode, state);

                    Console.WriteLine(myAreaCode);
                    Console.WriteLine();
                   // Console.ReadKey(true);
                }
                
                int key;
                int index;
                int[] data = new int[unsortedarray.Length];
                for (int i = 0; i < data.Length; i++)
                    data[i] = int.Parse(unsortedarray[i][0]);
            Console.Write("Enter the area code to search: ");
            key = int.Parse(Console.In.ReadLine());
            index = Search(data, key, 0, data.Length - 1);
            if (index == -1)
                Console.WriteLine("Area code {0} was not found.", key);
            else
                
                Console.WriteLine("Area code {0} is for this state: {1} ", key, index);
            
            }
            catch (IOException e)
            {
                Console.WriteLine(e);
            }
        }

        public static int Search(int[] data, int key, int left, int right)
        {
            if (left <= right)
            {
                int middle = (left + right) / 2;
                if (key == data[middle])
                    return middle;
                else if (key < data[middle])
                    return Search(data, key, left, middle - 1);
                else
                    return Search(data, key, middle + 1, right);
            }
            return -1;
        }
    }
}

Now it should be easy to show when index is for example 0, to show the actual state that is listed in the array at index 0...

That's great! If you feel your problem is resolved (does appear to be), please mark this thread as solved. Cheers!

Yes, now I got it. To get the state to show up, I just had to put the index at the first part of the multidimensional and [1] since that is the part of the multi-array where the states are in.

This question has already been answered. Start a new discussion instead.