0

I have a string array to be defined by user input and I need to convert the string array into a short array so I can use the values for calculations. I need to use an array because I will need to refer to all of the values collectively later. This is what I have:

string [] calIntake = new string[3];
calIntake [0] = Console.ReadLine ();
calIntake[1] = Console.ReadLine ();
calIntake[2] = Console.ReadLine ();

I have tried:

short[] calIntakeNum = Array.ConvertAll(calIntake.split(','), Short.Parse);

I get an error with this that says: "The type arguments for method 'System.Array.ConvertAll(TInput[], System.Converter)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Then I tried:

short[] calIntakeNum = Array.ConvertAll(calIntake.split(','), ne Converter<string, short>(Short.Parse));

and I get the same error. So how can I convert a string array based on user input into a short array?

Kyler Johnson
  • 1,939
  • 16
  • 14
  • Is this your exact code? `string[]` doesn't have a `split` method. – Asad Saeeduddin Nov 03 '15 at 01:32
  • Can you please clarify what do you expect `calIntake.split(',')` to do? Also why you are trying such a strange code when article that you've presumable read shows much simple code [Array.ConvertAll](https://msdn.microsoft.com/en-us/library/exc45z53(v=vs.110).aspx)? – Alexei Levenkov Nov 03 '15 at 01:51

3 Answers3

2

You can just project the strings through the short.Parse method:

short[] calIntakeNum = calIntake.Select(short.Parse).ToArray();
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • This is correct, though he shouldn't be using arrays at all for anything. If his instructor wants arrays, his instructor should be looking for a job at McDonalds. – 15ee8f99-57ff-4f92-890c-b56153 Nov 03 '15 at 02:02
  • list.Add(item) won't crash because the index is out of range. Arrays are appropriate when statically initialized (as OP's should have been). Otherwise, a generic List is more robust and maintainable. I know there is a school of thought which advocates always writing the worst code you can get away with. I'm not a fan. How long have you been writing code professionally? – 15ee8f99-57ff-4f92-890c-b56153 Nov 03 '15 at 13:12
  • I know you don't understand. I said "generic List" to distinguish it from System.Collections.List, which also called List and which is NOT generic. [Arrays are not generics](http://stackoverflow.com/a/15561100/424129). Your lack of experience has a lot to do with the fact that you don't know why a fixed-size container might be a problem. What if OP later needs to insert another item at index 1? He has to add a new line of code and change three others. Array is more work to write, much more work to alter, and both write and alter are more error prone. Several calls to list.Add() are preferable. – 15ee8f99-57ff-4f92-890c-b56153 Nov 03 '15 at 14:50
  • I mentioned a case where fixed size arrays are appropriate. Those cases are rare. OP's case is not one. An array in .NET is not a generic container class, but that's an irrelevant distraction you introduced for reasons of your own. OP's array initialization code is garbage that should not be taught to students. It's nice that you can research API history. Now learn programming. I'm done with you. – 15ee8f99-57ff-4f92-890c-b56153 Nov 03 '15 at 22:53
1

calIntake is already an array, you don't need to Split it. There is no type Short in C#, there is short or Int16

short[] calIntakeNum = Array.ConvertAll(calIntake, short.Parse);
Jakub Lortz
  • 14,616
  • 3
  • 25
  • 39
0

The second method you tried failed because you tried calling a method on a class that doesn't exist, and because Short.Parse doesn't exist. Removing the .split(',') from your first parameter to ConvertAll and changing Short.Parse to short.Parse will fix the issue:

short[] calIntakeNum = Array.ConvertAll(calIntake, new Converter<string, short>(short.Parse));

If it's possible for your program, you could declare your array as short[] originally, and call short.Parse on Console.ReadLine():

short[] shortArray = new short[3];
shortArray[0] = short.Parse(Console.ReadLine());
Shadow
  • 3,926
  • 5
  • 20
  • 41