if i want to read some integer what command i must use
how to do i read and save value in multi dimensional array??
urgently needed:-|
if i want to read some integer what command i must use
how to do i read and save value in multi dimensional array??
urgently needed:-|
The answers above show how to read a line and parse it, but both int.Parse and Convert.ToInt32 will throw on invalid input. A safer pattern is to keep prompting until the user enters a valid 32-bit integer using int.TryParse (docs).
static int ReadIntFromConsole(string prompt)
{
for (;;)
{
Console.Write(prompt);
var s = Console.ReadLine();
if (int.TryParse(s, out var value))
return value;
Console.WriteLine("Please enter a valid integer.");
}
}
// Fill a 2D array with integers read from the console
int rows = ReadIntFromConsole("Rows: ");
int cols = ReadIntFromConsole("Cols: ");
var grid = new int[rows, cols]; // C# multidimensional array
for (int r = 0; r < rows; r++)
for (int c = 0; c < cols; c++)
grid[r, c] = ReadIntFromConsole($"[{r},{c}] = "); Note: int[,] is a true multidimensional array; indexing is grid[r, c]. This is different from a jagged array (int[][]), which is an array of arrays. See Multidimensional arrays in C#.
If you are in ASP.NET, there is no keyboard/console. Read values from form fields (e.g., Request.Form["rows"]) and validate with int.TryParse, or use model binding with validation attributes. For multithreaded scenarios, avoid reading from the console on multiple threads, and synchronize writes to a shared array (e.g., with lock, docs).
Jump to Post— Ancient Dragon 5,243maybe this short tutorial will help you. Follow the link near the bottom of the page for other tutorials.
Jump to Post— blacklocist 0If your using a Console app it should be
string strTemp = Console.Readline()
Jump to Post— campkev 0int intTemp = Convert.ToInt32(Console.Readline())
maybe this short tutorial will help you. Follow the link near the bottom of the page for other tutorials.
maybe this short tutorial will help you. Follow the link near the bottom of the page for other tutorials.
that link is telling how to read from a file I want to know how to read from keyboard.
Like we have have cin command in c++ and scanf in C
If your using a Console app it should be
string strTemp = Console.Readline()
If your using a Console app it should be
string strTemp = Console.Readline()
but i said i wanted to read integer tht will read integer as character
int intTemp = Convert.ToInt32(Console.Readline())
int intTemp = Convert.ToInt32(Console.Readline())
i have also found one other way that is
int n= int.Parse(Console.ReadLine());
thanks pal that is great
Crap forgot that he wanted a integer and not a string. :)
Crap forgot that he wanted a integer and not a string. :)
no prob it is done now thanks for answering;)
hey these codes are working you can debug and use
int sum = 7;
Console.WriteLine("enter an integer for adding");
int a = int.Parse(Console.ReadLine());
sum += a;
Console.WriteLine(sum);
int no=(int)Convert.ToInt32(Console.ReadLine()); this ll also work
must given input in integer without string..how is it possible in c#?suppose user given input in string how can i manage?
Hi Jeya Rani, welcome to DaniWeb.
First: Don't resurrect old threads.
Start a new thread with your question and refer to this thread with a link, if you want to.
To answer you question, try this:
/// <summary>
/// Read an integer from the console
/// Return 0 if no success(perhaps MaxInt?)
/// </summary>
/// <param name="prompt">descriptive message</param>
/// <returns>an integer</returns>
public int readInt(string prompt)
{
Console.Write(prompt);
string line = Console.ReadLine();
int quantity;
if (int.TryParse(line, out quantity) == false)
{
quantity = 0;
}
return quantity;
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.