7

How can I take input in an array in C#?

I have written a code it's not working right. For example, if I input 1, then it gives an output 49.

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

namespace Google
{
    class Program
    {
        static void Main(string[] args)
        {
            int n;
            int[] array=new int[26];
            Console.Write("Enter number of cases : ");
            n = Console.Read();
            for (int i = 0; i < n; i++)
            {
                array[i] = Console.Read();
                Console.WriteLine( array[i]);
            }
            Console.Read();
        }  
    }
}
A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
Shubham Marathia
  • 176
  • 1
  • 2
  • 11
  • What does the debugger tell you? – Rowland Shaw Jul 22 '14 at 19:51
  • Let me see if I'm understanding your question properly, you're trying to store input from the command prompt into an array? – Ken Jul 22 '14 at 19:51
  • +1 for being a rookie and asking a nice question. Next thing start learning about [Debugging an application](http://msdn.microsoft.com/en-us/library/sc65sadd.aspx). – Habib Jul 22 '14 at 19:52
  • I'm not saying don't help the guy, but this type of question is generally flagged. I guess SO gets sentimental sometimes. – Jonesopolis Jul 22 '14 at 19:57
  • Possible duplicate of https://stackoverflow.com/questions/26622240/c-sharp-user-input-int-to-array – Pie Jun 14 '19 at 19:09

8 Answers8

6
arr = Array.ConvertAll(Console.ReadLine().Trim().Split(' '),Convert.ToInt32);
double-beep
  • 5,031
  • 17
  • 33
  • 41
Sayak Sinha
  • 61
  • 1
  • 1
  • 1
    Hi, welcome to Stack Overflow. Please [edit] your answer to explain the code. Thanks! – grooveplex Jun 14 '19 at 19:27
  • This is what I wanted. Thanks – Keerthi Nandigam Jun 24 '21 at 05:41
  • He is basically, trying to split the string input `Console.ReadLine().Trim().Split(' ')`, which is in this form: "7 4". This will output the string array. But he wanted int array, so he has used Array.ConvertAll() to convert to Int32. Array.ConvertAll() method will convert array of one type to array of another type. – KushalSeth Apr 27 '22 at 09:01
3

Console.Read returns the character code, not the number you entered.

Use int.Parse(Console.ReadLine()) instead:

n = int.Parse(Console.ReadLine());
//...
array[i] = int.Parse(Console.ReadLine());
pascalhein
  • 5,700
  • 4
  • 31
  • 44
3

49 is correct. this number is coming for the ascii value of the character "1" Source (http://www.asciitable.com/)

You need to include a parser for your int.

As Selman22 said:

array[i] = int.Parse(Console.ReadLine());

will work for you.

David
  • 231
  • 1
  • 8
3

Most of the competitive programming take inline integer input as the input array. In that case console input can do this way:

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

namespace CSharp
{
    class Program
    {
        static void Main(string[] args)
        {

            int n;
            n =Int32.Parse(Console.ReadLine());
            int[] arr = new int[n];
            string[] s = Console.ReadLine().Split(' ');

            for (int i= 0;i< n;i++)
            {
                arr[i] = Int32.Parse(s[i]);
            }
            Console.WriteLine(n);
            foreach (var item in arr)
            {
                Console.Write(item+" ");
            }
        }
    }
}
2

Console.Read method gets the next character from input stream, and converts it to integer value which is the ASCII value of the char. You want Console.ReadLine instead:

array[i] = int.Parse(Console.ReadLine());

Use int.TryParse if you want to validate user's input.


Btw it can be done with Read method if you want to get just numbers from 0 to 9 (which probably you don't), but the code will look ugly:

 array[i] = int.Parse(((char)Console.Read()).ToString());
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
2

One liner solution:

var array = Console.ReadLine().Split().Select(int.Parse).ToArray();

Explanation

the array will be an array of integers read from the console input in one line separated by space.

Example Input: "1 2 3 4 5".

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
1

You are reading a char, not a number, in your case it is returning the ASCII value of 1, which is 49. You should use proper parsing functions like Int32.Parse(Console.ReadLine()).

LazyOfT
  • 1,428
  • 7
  • 20
1

1 coming across as 49 should be your hint. 49 is the ASCII value for the character '1'.

So what's happening is that your Console.Read() call is returning a char which is being implicitly cast as an integer into your integer array.

You probably actually expect the user to type a number and hit enter. So you'd probably be better off using Console.ReadLine() and then using int.TryParse on the string you get from that.

Tim
  • 14,999
  • 1
  • 45
  • 68