|
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
int size = Convert.ToInt32(Console.ReadLine());
&
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
Console.Write("Wrong input, only numbers");
My code:
Console.Write("How many items in the array ");
int size = Convert.ToInt32(Console.ReadLine());
int[] array = new int[size];
Console.WriteLine("Enter {0} array items", size);
for (int i = 0; i < size; i++)
{
array[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("\nThe following items are in the array: ");
for (int i = 0; i < size; i++)
{
Console.Write("{0} ", array[i]);
}
Console.ReadLine();
|
|
|
|
|
Yeah, you cannot convert strings with letters to an integer since there is no logical conversion.
The documentation on Integer.TryParse can be found here[^] complete with examples and you can even run the example code in the browser and play around with it.
|
|
|
|
|
All fine. Thanks.
modified 26-Mar-22 18:18pm.
|
|
|
|
|
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
So how to generate 5 random numbers that fall into the range between 0 and 10000 and assign them to an array? Thanks.
|
|
|
|
|
Think about it: how do you generate one random number? Hint: see the Random class ...
Get that working, and you can then generate as many as you need.
Get that working, and you can store them in an array.
Get that working, and you can start on the rest of the task.
As the link I originally gave you explains: take a "difficult task" and break it into smaller ones. The smaller ones are "doable" on their own, and doing all of those means you do the "impossible" task as well!
Read the article, and see how far you can get.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I made the initial code as below:
using System;
public class Program
{
public static void Main(string[] args)
{
int N = 10;
int[] array = new int[N];
Random rnd = new Random();
for (int i = 0; i < N; i++)
{
array[i] = rnd.Next(10000);
}
for (int j = 0; j < N; j++)
{
Console.Write(array[j])
}
}
}
But it fails to display any code. Can you please help? Thanks.
|
|
|
|
|
Chances are it does, but it closes down too fast and you can't see it ...
Run it in the debugger, and put a breakpoint on the close curly bracket at the end of the Main method. When you run it, it will stop at the breakpoint and you can see what has happened (or run it again and step through to see what it does while it is running!)
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
It works now. The semicolon was missing.
|
|
|
|
|
If you get an error, it helps if you tell us that, than just assuming we will have seen that on your screen!
You will be solving syntax errors often - we all do - so this may help you get started: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Please do not delete your original question, as the full thread could be helpful to other people. Just add "[Solved]" to the message's title.
|
|
|
|
|
My insert code is not working fine, bcz if i click radionbutton2 and checkbox 2 means it was inserting data automatically radionbutton1 and checkbox1 only, actually if which textbox is selected that data should enter null value radionbutton data should not saved in db
if (!string.IsNullOrEmpty(textBox11.Text) && !string.IsNullOrEmpty(textBox12.Text) && !string.IsNullOrEmpty(textBox13.Text) && !string.IsNullOrEmpty(textBox14.Text) && (radioButton1.Checked || radioButton2.Checked) && (checkBox1.Checked || checkBox2.Checked))
{
string connectionString;
MySqlConnection cnn;
connectionString = @"Data Source=localhost;Initial Catalog=testDB;User ID=root;Password=mysql";
cnn = new MySqlConnection(connectionString);
cnn.Open();
string id = textBox14.Text;
string name = textBox11.Text;
string year = textBox12.Text;
string quality = radioButton1.Text == "" ? radioButton2.Text : radioButton1.Text;
string taste = checkBox1.Text == "" ? checkBox2.Text : checkBox1.Text;
string sales = textBox13.Text;
textBox11.Text = "";
textBox12.Text = "";
textBox13.Text = "";
textBox14.Text = "";
radioButton1.Text = "";
radioButton2.Text = "";
checkBox1.Text = "";
checkBox2.Text = "";
string query = "INSERT INTO fruits VALUES(@fruitsid, @fruitsname, @fruitsyear, @quality, @taste, @sales)";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Parameters.AddWithValue("@fruitsid", Convert.ToInt32(id));
cmd.Parameters.AddWithValue("@fruitsname", name);
cmd.Parameters.AddWithValue("@fruitsyear", year);
cmd.Parameters.AddWithValue("@quality", quality);
cmd.Parameters.AddWithValue("@taste", taste);
cmd.Parameters.AddWithValue("@sales", sales);
cmd.Connection = cnn;
cmd.ExecuteNonQuery();
MessageBox.Show("Record Inserted Successfully");
DisplayData();
cnn.Close();
}
}
else
{
MessageBox.Show("Please Fill Data");
}
|
|
|
|
|
Quote: My insert code is not working fine, bcz if i click radionbutton2 and checkbox 2 means it was inserting data automatically radionbutton1 and checkbox1 only, actually if which textbox is selected that data should enter null value radionbutton data should not saved in db
The question is meaningless, you still haven't learned how to use a debugger, and you are still trying to get others to fix problems instead of thinking about what is actually going on.
what have you tried to work out what the problem might be, given that we can't understand your "explanation" of the problem, run your code, see your inputs, or access your DB?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
And you are still posting a success message even if it fails.
cmd.ExecuteNonQuery();
MessageBox.Show("Record Inserted Successfully");
|
|
|
|
|
Optimism is a moral duty...
Luc Pattyn [My Articles]
The Windows 11 "taskbar" is disgusting. It should be at the left of the screen, with real icons, with text, progress, etc. They downgraded my developer PC to a bloody iPhone.
|
|
|
|
|
I'm sorry but i don't understand your question. One thought I have - are you trying to do an 'exclusive or'? In other words, are you trying to see if button 1 is selected, or button 2 is selected, but not both at the same time?
As others have pointed out a few different times in response to your questions - you cannot assume a SQL command is successful just because control moves to the next statement. You need to check to see if you were able to perform the SQL statement. You may lose your connection, you may not be authorized to the database, the update may have failed to run. There are ways to see if the SQL statement worked before you proceed to posting a 'success' message.
|
|
|
|
|
yes sir need to select any one at a time. and i am using mysql not sql
|
|
|
|
|
You will need to research how to do an "exclusive or" to learn the syntax.
As for my SQL comment - it still applies to MySQL. You must learn how to tell if the SQL command ran successfully. You cannot assume that it ran as desired just because control flows to the next statement. This is why others are telling you to check for sucess on your SQL operation before you proceed to the statement that says it succeeded.
When you become a professional programmer, you need to worry about things like "Do I have authority to access the database? Is the database online? Did some other unexpected condition occur?" Much of programming is dealing with gracefully handling errors and unexpected conditions.
CodeProject members are professionals and their tips and suggestions are to help you achieve that skill level.
|
|
|
|
|
hi all
how i get the dpi scale factor is selected.
thanks.
|
|
|
|
|
Where are you going to get it from?
|
|
|
|
|
|
var graphics = control.CreateGraphics())
int dpi_scale_val= graphics.DpiX;
but it always return 96
|
|
|
|
|
Le@rner wrote: but it always return 96 That is correct.
|
|
|
|
|
i have 4k laptop its by default resolution is 200%
|
|
|
|
|
If you want help with a programming question, you really need to provide proper details, rather than vague statements.
|
|
|
|