Nicholas
Nicholas

Reputation: 793

C# Crashing if no input for string Array

I Have a string array with 5 values, i want the program to loop even though i don't enter a value for the arrays. If I split the arrays without inserting anything (pressing ..... (5 times "." to split the array) then it doesn't crash it will just loop. But if i just hit enter, then the program crashes.

Is there a way to fix the loop so that even though there is no kind of input, that it won't crash? (It also crashes if you don't complete all 5 values.)

Net = Console.ReadLine();
            string[] oktet = new string[5]; 
            oktet = Net.Split('.', '/'); 

            temp = oktet[0]; //inputs value of array in temp
            NaN = int.TryParse(temp, out Net0);
            temp = oktet[1];
            NaN = int.TryParse(temp, out Net1);
            temp = oktet[2];
            NaN = int.TryParse(temp, out Net2);
            temp = oktet[3];
            NaN = int.TryParse(temp, out Net3);
            temp = oktet[4];
            NaN = int.TryParse(temp, out subnet);
        }
        while (!NaN | Net0 > 255 | Net0 < 0 | Net1 > 255 | Net1 < 0 | Net2 > 255 | Net2 < 0 | Net3 > 255 | Net3 < 0 | subnet > 32 | subnet < 0);

I know it's pretty amateur, but hey, we're here to learn right? :)

Thanks in advanced!

Upvotes: 2

Views: 601

Answers (4)

Yochai Timmer
Yochai Timmer

Reputation: 49231

The problem is that your variable oktet isn't an array of 5, because you're assigning something else in it.

  string[] oktet = new string[5]; // Assigns an array of 5
  oktet = Net.Split('.', '/'); //assigns the result of the split to the variable

So, oktet is has the result of the split, and its length.

BTW, Classes start with a capital letter, and the variables should start with a small letter.
Coding standards will make the code more readable, and will help you tell the difference between items.

Upvotes: 0

Danny Varod
Danny Varod

Reputation: 18069

var userInput = Console.ReadLine();

var userInputSplit = userInput.Split('.', '/');

var numbers = userInputSplit.Select(word =>
    {
        int result;
        if (byte.TryParse(word, out result))
            return (byte?)result;
        return (byte?)null; });
    });

var inputComplete = number.Where(number => number.HasValue).Count() == 4;

Upvotes: 0

Luke Hutton
Luke Hutton

Reputation: 10722

I would just use some command line option library if you could (unless this is homework where you need to learn how to validate input, parse, etc.). See NDesk.Options (http://www.ndesk.org/Options), available via Nuget as well. See the following for required options: How to enforce required command-line options with NDesk.Options?

Upvotes: 0

Paul Sasik
Paul Sasik

Reputation: 81429

You can try to do something like this:

string[] oktet = Net.Split('.', '/'); // size array according to input

if (oktet.Length != 5) continue; // reloop on bad input

Those two lines of code replace these:

        string[] oktet = new string[5]; 
        oktet = Net.Split('.', '/'); 

Upvotes: 2

Related Questions