-
May 4th, 2013, 04:17 AM
#1
Program help
Im trying to take the numbers from a file display them in a text box and then do calculations with each one which are finding the biggest number out of them, the smallest number and the average. right now i havecreated the form and added the open file to the menu strip and have gotten to load the numbers from the text file into my list box but i cant figure out how to seperate each number individually on their own line in the list box.. here's the code if in anyone is interested in helping. Thanks!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Xml;
using System.Runtime;
using System.Xml.Serialization;
namespace assignment4
{
public partial class Form1 : Form
{
List<double> numbers;
public Form1()
{
InitializeComponent();
numbers = new List<double>();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
listBox1.Items.Clear();
textBox1.Clear();
string filenumbers;
System.IO.StreamReader sr = new
System.IO.StreamReader(openFileDialog1.FileName);
filenumbers = (sr.ReadToEnd());
listBox1.Items.Add(filenumbers.Split(' '));
sr.Close();
}
string text = "";
foreach (var item in listBox1.Items)
{
text += item.ToString();
}
textBox1.Text = text;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
public double getAverage()
{
double avg = 0.0;
foreach (double temp in numbers)
{
avg += temp;
}
return (avg / numbers.Count());
}
}
}
-
May 5th, 2013, 02:08 AM
#2
Re: Program help
Just your listbox problem should be fixed by:
Code:
listBox1.Items.AddRange(filenumbers.Split(' '));
To do the math calculations you want though, you'll probably want to parse them from strings into doubles:
Code:
string[] numberStrings = filenumbers.Split(' ');
double[] numbers = new double[numberStrings.Length];
for(int i = 0; i < numberStrings.Length; i++)
{
numbers[i] = Double.Parse(numberStrings[i]); //Convert the string to a double
}
That parse method also works with other types (e.g. integers, using Int32.Parse(...)). An error checking version is Double.TryParse(string, out double), but to use it you'll need to read about out parameters if you want to use it http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx
Also, please surround code snippets with [code] and [/code] tags, to preseve formatting and increase the probability of a reply.
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|