I\'m asking the user to input 5 numbers and store it into an array so I can send the values in a method and subtract 5 numbers from each array. When I use the:
furkle is correct, it's not working because the console.ReadLine method returns a string, and you can't assign a string to an int array. However, the solution provided is a bit clunky because it only reads one character at a time from the console. It's better to take in all the input at once.
Here is a short console program that
Error handling is included.
static void Main(string[] args){
var intList = new List<int>();
string sUserInput = "";
var sList = new List<string>();
bool validInput = true;
do
{
validInput = true;
Console.WriteLine("input a space separated list of integers");
sUserInput = Console.ReadLine();
sList = sUserInput.Split(' ').ToList();
try
{
foreach (var item in sList) {intList.Add(int.Parse(item));}
}
catch (Exception e)
{
validInput = false;
Console.WriteLine("An error occurred. You may have entered the list incorrectly. Please make sure you only enter integer values separated by a space. \r\n");
}
} while (validInput == false);
Console.WriteLine("\r\nHere are the contents of the intList:");
foreach (var item in intList)
{
Console.WriteLine(item);
}
Console.WriteLine("\r\npress any key to exit...");
Console.ReadKey();
}//end main
If you want to make sure the user only enters a total of 5 integers you can do the DO WHILE loop like this:
do
{
validInput = true;
Console.WriteLine("input a space separated list of integers");
sUserInput = Console.ReadLine();
sList = sUserInput.Split(' ').ToList();
if (sList.Count > 5)
{
validInput = false;
Console.WriteLine("you entered too many integers. You must enter only 5 integers. \r\n");
}
else
{
try
{
foreach (var item in sList) { intList.Add(int.Parse(item)); }
}
catch (Exception e)
{
validInput = false;
Console.WriteLine("An error occurred. You may have entered the list incorrectly. Please make sure you only enter integer values separated by a space. \r\n");
}
}
} while (validInput == false);
You initialize the array first
int[] val = new int[5];
and then when you do a for loop:
for (int i=0; i<val.Length; i++)
{
val[i] = int.Parse(Console.ReadLine());
}
Hint: To send the array values from main to the method, just look at how static void main
is formed:
static void Main(string[] args)
Meaning main is accepting a string array of of console arguments.
You just need to parse the string
being entered from the console to int first.You accept int by doing:
for(int i=0;i<myArray.Length;i++) {
myArray[i] = int.Parse(Console.ReadLine()); //Int32.Parse(Console.ReadLine()) also works
Just like @Furkle said, it is always best to use TryParse()
which enables you to perform exception handling. MSDN has a nice tutorial on this.
You're assigning the string input from the Console to an array of int. This is wrong for two reasons:
int
arrays (int[]
) are a collection of ints
.int
and needs to be parsed first.This is a slight diversion, but I also don't think you should be using an array. Arrays generally have significantly less function in C# than the built-in List<T>
. (All the rockstars chime in here) Some people say there's no reason to use arrays at all - I won't go that far, but I think for this use case it'll be a lot easier to just add items to a List
than to allocate 5 spaces and initialize values by index.
Either way, you should use int.TryParse()
, not int.Parse()
, given that you'll immediately get an exception if you don't check if the user input was parseable to int
. So example code for taking in strings from the user would look like this:
List<int> userInts = new List<int>();
for (int i = 0; i < 5; i++)
{
string userValue = Console.ReadLine();
int userInt;
if (int.TryParse(userValue, out userInt))
{
userInts.Add(userInt);
}
}
If you'd still like to use the array, or if you have to, just replace List<int> userInts
... with int[] userInts = new int[5];
, and replace userInts.Add(userInt)
with userInts[i] = userInt;
.