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();
        }  
    }
}
5
  • What does the debugger tell you? Commented Jul 22, 2014 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? Commented Jul 22, 2014 at 19:51
  • +1 for being a rookie and asking a nice question. Next thing start learning about Debugging an application. Commented Jul 22, 2014 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. Commented Jul 22, 2014 at 19:57
  • Possible duplicate of stackoverflow.com/questions/26622240/… Commented Jun 14, 2019 at 19:09

8 Answers 8

6
arr = Array.ConvertAll(Console.ReadLine().Trim().Split(' '),Convert.ToInt32);
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, welcome to Stack Overflow. Please edit your answer to explain the code. Thanks!
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.
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());

Comments

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.

Comments

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+" ");
            }
        }
    }
}

Comments

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());

2 Comments

Another ugly approach: (int)char.GetNumericValue((char)Console.Read())
A hacky approach: Console.Read() - '0'
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".

Comments

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()).

Comments

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.

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.