0

throwing a 'System.IndexOutOfRangeException' at me in a place it hadn't a year or two ago when i first made this. I asked my current proffessor but he just said "it works there is no problem" We are both getting the exception so idk why the teacher isn't. Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/* Author: Austin Bigge
* This program will ask for a ten digit alphanumeric
* number and return the numeric number.
*/
namespace alphanumeric
{
class Program
{
    static void Main(string[] args)
    {
        // Switch for alphanumeric entry
        string numberString; //alphanumberic entry
        char currentLetter;
        string invalue;
        double pnumber; //returned single numeric phone number
        int i; //i = the current character space

        //User prompt
        Console.WriteLine("This program will ask for an alphanumberic phone ");
        Console.WriteLine("number and change it into a numeric phone number.");
        Console.WriteLine("! Alphanumeric characters only !");
        Console.WriteLine("! No Spaces, Dashes, or Symbols !");
        Console.Write("What is the 10 digit phone number? ");
        invalue = Console.ReadLine();
        numberString = invalue;

        char[] array = numberString.ToCharArray();
        for (i = 0; i < array.Length; i++) //for loop
        // gets the current letter at position "i"
        currentLetter = array[i];
        switch (i) //case switch for possible entries
        {
            case '1':
                pnumber = 1; Console.WriteLine(pnumber); break;
            case '2':
            case 'a':
            case 'A':
            case 'b':
            case 'B':
            case 'c':
            case 'C':
                pnumber = 2; Console.WriteLine(pnumber); break;
            case '3':
            case 'd':
            case 'D':
            case 'e':
            case 'E':
            case 'f':
            case 'F':
                pnumber = 3; Console.WriteLine(pnumber); break;
            case '4':
            case 'g':
            case 'G':
            case 'h':
            case 'H':
            case 'i':
            case 'I':
                pnumber = 4; Console.WriteLine(pnumber); break;
            case '5':
            case 'j':
            case 'J':
            case 'k':
            case 'K':
            case 'l':
            case 'L':
                pnumber = 5; Console.WriteLine(pnumber); break;
            case '6':
            case 'm':
            case 'M':
            case 'n':
            case 'N':
            case 'o':
            case 'O':
                pnumber = 6; Console.WriteLine(pnumber); break;
            case '7':
            case 'p':
            case 'P':
            case 'q':
            case 'Q':
            case 'r':
            case 'R':
            case 's':
            case 'S':
                pnumber = 7; Console.WriteLine(pnumber); break;
            case '8':
            case 't':
            case 'T':
            case 'u':
            case 'U':
            case 'v':
            case 'V':
                pnumber = 8; Console.WriteLine(pnumber); break;
            case '9':
            case 'w':
            case 'W':
            case 'x':
            case 'X':
            case 'y':
            case 'Y':
            case 'z':
            case 'Z':
                pnumber = 9; Console.WriteLine(pnumber); break;
            case '0':
                pnumber = 0; Console.WriteLine(pnumber); break;
            case ' ': Console.WriteLine("Bad Value, do not use spaces"); break;
            default: Console.WriteLine("Bad Value, use only numbers or letters."); break;


        }

    }
}

}

any help is appreciated, he keeps asking me for help and i keep telling him he knows just as much as me. p.s. the debugger pointed to line 36 for this exception

EDIT:

yup you're right, now i have an unassigned variable at line 37 "currentLetter", i already initialized it i thought, when i tried to initialize it at the error it told me it was already initialized... i must have deleted my working program. i found a couple empty iterations of this file. I really appreciate the help however, and so will my friend im sure.

i think i fixed it, i replaced "currentLetter" with "i"

and now the array isn't working. only returning the default case. stuck again. i edited my progress.

EDIT

Found out a bunch of errors when we put our heads together. Completed program is as follows.

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

namespace ConsoleApplication1
{
    class Program
    {
    static void Main(string[] args)
    {
        // Switch for alphanumeric entry
        string numberString; //alphanumberic entry
        char currentLetter;
        string invalue;
        double pnumber; //returned single numeric phone number
        int i; //i = the current character space

        //User prompt
        Console.WriteLine("This program will ask for an alphanumberic phone ");
        Console.WriteLine("number and change it into a numeric phone number.");
        Console.WriteLine("! Alphanumeric characters only !");
        Console.WriteLine("! No Spaces, Dashes, or Symbols !");
        Console.Write("What is the 10 digit phone number? ");
        invalue = Console.ReadLine();
        numberString = invalue;

        char[] array = numberString.ToCharArray();
        for (i = 0; i < 10; i++)
        {
            //for loop
            //numberString = invalue.ToString().ToCharArray()[i]);
            // gets the current letter at position "i"
            currentLetter = array[i];
            switch (currentLetter) //case switch for possible entries
            {
                case '0':
                    pnumber = 0; Console.Write(pnumber); break;

                case '1':
                    pnumber = 1; Console.Write(pnumber); break;
                case '2':
                case 'a':
                case 'A':
                case 'b':
                case 'B':
                case 'c':
                case 'C':
                    pnumber = 2; Console.Write(pnumber); break;
                case '3':
                case 'd':
                case 'D':
                case 'e':
                case 'E':
                case 'f':
                case 'F':
                    pnumber = 3; Console.Write(pnumber); break;
                case '4':
                case 'g':
                case 'G':
                case 'h':
                case 'H':
                case 'i':
                case 'I':
                    pnumber = 4; Console.Write(pnumber); break;
                case '5':
                case 'j':
                case 'J':
                case 'k':
                case 'K':
                case 'l':
                case 'L':
                    pnumber = 5; Console.Write(pnumber); break;

                case '6':
                case 'm':
                case 'M':
                case 'n':
                case 'N':
                case 'o':
                case 'O':
                    pnumber = 6; Console.Write(pnumber); break;

                case '7':
                case 'p':
                case 'P':
                case 'q':
                case 'Q':
                case 'r':
                case 'R':
                case 's':
                case 'S':
                    pnumber = 7; Console.Write(pnumber); break;

                case '8':
                case 't':
                case 'T':
                case 'u':
                case 'U':
                case 'v':
                case 'V':
                    pnumber = 8; Console.Write(pnumber); break;

                case '9':
                case 'w':
                case 'W':
                case 'x':
                case 'X':
                case 'y':
                case 'Y':
                case 'z':
                case 'Z':
                    pnumber = 9; Console.Write(pnumber); break;

                case ' ':
                    Console.WriteLine();
                    Console.WriteLine("Bad Value, do not use spaces");
                    Console.Write("What is the 10 digit phone number? ");
                    invalue = Console.ReadLine();
                    numberString = invalue;
                    array = numberString.ToCharArray();
                    i = 0;

                    break;
                default:
                    Console.WriteLine();
                    Console.WriteLine("Bad Value, use only numbers or letters.");
                    Console.Write("What is the 10 digit phone number? ");
                    invalue = Console.ReadLine();
                    numberString = invalue;
                    array = numberString.ToCharArray();
                    i = 0;
                    break;

            }

        }
        Console.WriteLine();
        Console.WriteLine("Press any key to close this window");
        Console.ReadLine();
    }
}
}

Thank you for getting us started with the fixes, we both appreciate it, friend!

0

1 Answer 1

2

The semi-colon on your for line is what's causing your problem:

for (i = 0; i < array.Length; i++) ; //for loop

A for loop should be followed by a block, but in this case, since it's followed by a semi-colon, the code that follows isn't part of the loop, but just regular code that's executed once.

So the for loop executes with an empty body, and when it's done, i has a value of the length of the array, which is of course one higher than the max index, which throws that exception.

To fix this, create a block (curly brackets) around the group of code you want included in the loop.

This also brings up a separate issue - for loops generally will declare the indexer in the loop itself - if you would have defined i in the for, then it would have been out of scope for the following lines, and would have been easier to see your mistake, since it would have been a compiler error.

for (int i = 0; i < array.Length; i++)
{
    // Your code here
}
// i is no longer in scope
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.