Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to add numbers which are in richtextbox to an array?

Example:
4
2
15
48
5.9
7.3
14.98
13
24

These numbers I want to add in an double array, can you tell me how I do that?

What I have tried:

I tried some things but nothing
Posted
Updated 3-Jun-20 10:50am

1 solution

Get each line separately, and use double.TryParse[^] to convert each value individually to a double variable.
You can then add each double to your array, either by working out in advance how many elements you will need - the number of lines will tell you that - and using an index to step though it as you convert, or by using a List<double> and it's Add method.
 
Share this answer
 
Comments
El_Pitufo 4-Jun-20 8:14am    
If it is not a form application but console, how can i add numbers from notepad.txt in an array using StreamReader? If you can give me a code
OriginalGriff 4-Jun-20 8:27am    
Read the file, then follow the above instructions.
Hint: Look at the File.ReadAllLines method.
El_Pitufo 4-Jun-20 8:26am    
StreamReader r = new StreamReader("example.txt", Encoding.GetEncoding("windows-1251"));
double[] arr = new double[100];
int i = 0;
string line;
while ((line = r.ReadLine()) != null)
{
mas[i] = double.Parse(line);
i = i + 1;

}
r.Close();
Console.WriteLine(mas);
Console.ReadLine();

This is my code for an array, but it's not working.
How to change it to work?

And how to Console.WriteLine the array?
and then from this array how to Console.WrtieLine the numbers bigger than 15 for example, and how to save them into another notepad.txt

If you help me with this I will be very thankful.
OriginalGriff 4-Jun-20 9:02am    
What have you tried to get it working?
What did the debugger show you?
What is it doing that you didn't expect?
What is "mas"?
El_Pitufo 4-Jun-20 11:51am    
I forgot to change it to "arr". mas = arr xD
I tried this code, there is no errors. Debugger show me "System.Int32[]"

StreamReader r = new StreamReader("example.txt", Encoding.GetEncoding("windows-1251"));
double[] arr = new double[100];
int i = 0;
string line;
while ((line = r.ReadLine()) != null)
{
arr[i] = double.Parse(line);
i = i + 1;

}
r.Close();
Console.WriteLine(arr);
Console.ReadLine();

^ without "mas"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900