Click here to Skip to main content
15,795,331 members
Home / Discussions / C#
   

C#

 
AnswerRe: Access Android Tablet from C# Pin
Richard MacCutchan30-Mar-22 23:30
mveRichard MacCutchan30-Mar-22 23:30 
GeneralRe: Access Android Tablet from C# Pin
Kevin Marois31-Mar-22 7:15
professionalKevin Marois31-Mar-22 7:15 
GeneralRe: Access Android Tablet from C# Pin
Richard MacCutchan31-Mar-22 7:19
mveRichard MacCutchan31-Mar-22 7:19 
AnswerRe: Access Android Tablet from C# Pin
OriginalGriff31-Mar-22 4:09
mvaOriginalGriff31-Mar-22 4:09 
GeneralRe: Access Android Tablet from C# Pin
Kevin Marois31-Mar-22 7:16
professionalKevin Marois31-Mar-22 7:16 
GeneralRe: Access Android Tablet from C# Pin
Kevin Marois31-Mar-22 9:11
professionalKevin Marois31-Mar-22 9:11 
GeneralRe: Access Android Tablet from C# Pin
OriginalGriff31-Mar-22 20:49
mvaOriginalGriff31-Mar-22 20:49 
QuestionError when parsing string as int Pin
Member 1558328029-Mar-22 6:19
Member 1558328029-Mar-22 6:19 
Hi all,

I am receiving errors when executing below code.
The code asks me how many items the array has, and then let me fill the array with input.
The problem is whenever I input letters instead of numers I get an exception.
System.FormatException: 'Input string was not in a correct format.

The error happens at the following lines
C#
int size = Convert.ToInt32(Console.ReadLine());

&
C#
array[i] = Convert.ToInt32(Console.ReadLine());


Now I know that I might be using the int.TryParse method, by I do not know how to implement it.
Of perhaps when the input is not valid, give an error like
C#
Console.Write("Wrong input, only numbers");


My code:
C#
//Ask how many array items need to be present
Console.Write("How many items in the array ");
int size = Convert.ToInt32(Console.ReadLine());
int[] array = new int[size];

//Enter array items
Console.WriteLine("Enter {0} array items", size);
for (int i = 0; i < size; i++)
{
    array[i] = Convert.ToInt32(Console.ReadLine());
}

// Print array items
Console.Write("\nThe following items are in the array: ");
for (int i = 0; i < size; i++)
{
    Console.Write("{0} ", array[i]);
}
Console.ReadLine();


AnswerRe: Error when parsing string as int Pin
Dave Kreskowiak29-Mar-22 6:33
mveDave Kreskowiak29-Mar-22 6:33 
QuestionFind the smallest integer that is not in a random array Pin
Member 1204230226-Mar-22 9:01
Member 1204230226-Mar-22 9:01 
AnswerRe: Find the smallest integer that is not in a random array Pin
OriginalGriff26-Mar-22 9:22
mvaOriginalGriff26-Mar-22 9:22 
GeneralRe: Find the smallest integer that is not in a random array Pin
Member 1204230226-Mar-22 9:29
Member 1204230226-Mar-22 9:29 
GeneralRe: Find the smallest integer that is not in a random array Pin
OriginalGriff26-Mar-22 9:46
mvaOriginalGriff26-Mar-22 9:46 
GeneralRe: Find the smallest integer that is not in a random array Pin
Member 1204230226-Mar-22 10:51
Member 1204230226-Mar-22 10:51 
GeneralRe: Find the smallest integer that is not in a random array Pin
OriginalGriff26-Mar-22 12:43
mvaOriginalGriff26-Mar-22 12:43 
GeneralRe: Find the smallest integer that is not in a random array Pin
Member 1204230226-Mar-22 13:17
Member 1204230226-Mar-22 13:17 
GeneralRe: Find the smallest integer that is not in a random array Pin
OriginalGriff26-Mar-22 21:05
mvaOriginalGriff26-Mar-22 21:05 
AnswerRe: Find the smallest integer that is not in a random array Pin
Richard MacCutchan26-Mar-22 23:32
mveRichard MacCutchan26-Mar-22 23:32 
QuestionInserting Through Radionbutton and checkbox Pin
N Mohamed rafi26-Mar-22 0:20
N Mohamed rafi26-Mar-22 0:20 
AnswerRe: Inserting Through Radionbutton and checkbox Pin
OriginalGriff26-Mar-22 0:55
mvaOriginalGriff26-Mar-22 0:55 
AnswerRe: Inserting Through Radionbutton and checkbox Pin
Richard MacCutchan26-Mar-22 1:19
mveRichard MacCutchan26-Mar-22 1:19 
GeneralRe: Inserting Through Radionbutton and checkbox Pin
Luc Pattyn26-Mar-22 10:24
sitebuilderLuc Pattyn26-Mar-22 10:24 
AnswerRe: Inserting Through Radionbutton and checkbox Pin
Craig Robbins26-Mar-22 4:36
Craig Robbins26-Mar-22 4:36 
GeneralRe: Inserting Through Radionbutton and checkbox Pin
N Mohamed rafi26-Mar-22 6:16
N Mohamed rafi26-Mar-22 6:16 
GeneralRe: Inserting Through Radionbutton and checkbox Pin
Craig Robbins26-Mar-22 13:42
Craig Robbins26-Mar-22 13:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.