-
August 4th, 2010, 03:04 AM
#1
[Solved][Homework] Reading and Writing to file
I'm on the verge of completing my program. Hers what i have so far. Everything should be okay. I just can't get the write to file and read to file working correctly.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Project_2
{
/* this class is used to test class NsgUI and Nsg
* */
class Test
{
static void Main(string[] args)
{
new NsgUI();
}
}
/* stores the stationDesignation, cantactPerson, and temperature
* */
class station
{
private string designation, contactName;
private double temp, airQualityIndex, pollenCountValue;
public station() { }
public station(string stationDesignation, string contactPerson, double temperature, double count, double score)
{
designation = stationDesignation;
contactName = contactPerson;
temp = temperature;
airQualityIndex = score;
pollenCountValue = count;
}
public void setDesignation(string stationDesignation)
{ designation = stationDesignation; }
public void setContactPerson(string contactPerson)
{ contactName = contactPerson; }
public void setTemperature(double temperature)
{ temp = temperature; }
public void setPollenCount(double count)
{ pollenCountValue = count; }
public void setAirQuality(double score)
{ airQualityIndex = score; }
public string getDesignation()
{ return designation; }
public string getContactName()
{ return contactName; }
public double getTemperature()
{ return temp; }
public double getPollenCount()
{ return pollenCountValue; }
public double getAirQuality()
{ return airQualityIndex; }
public static explicit operator string(station name)
{
string FormattedOutput;
FormattedOutput = "Weather Station " + name.designation + ": \t" + name.temp + "\t\t" + ((5 * (name.temp - 32)) / 9).ToString("#0.00") + "\n" +
"Contact Person: \t" + name.contactName + "\n" +
"---------------------------------------------------------\n";
return FormattedOutput;
}
/*public static explicit operator string(station pollenCount)
{
string FormattedOutput;
FormattedOutput = "Weather Station " + pollenCount.designation + ": \t" + pollenCount.pollenCountValue + "\n" +
"---------------------------------------------------------\n";
return FormattedOutput;
}
public static explicit operator string(station airQuality)
{
string FormattedOutput;
FormattedOutput = "Weather Station " + airQuality.designation + ": \t" + airQuality.airQualityIndex + "\n" +
"---------------------------------------------------------\n";
return FormattedOutput;
}*/
};
class stationList
{
private string fileName;
private List<station> littleList;
double highTemperature, lowTemperature, mean, total = 0;
public stationList()
{
littleList = new List<station>();
fileName = "DEFAULT.DEFAULT";
}
public void Add(string designation, string contactName, double temperature, double count, double score)
{
littleList.Add(new station(designation, contactName, temperature, count, score));
}
public void meanCalculation()
{
for (int i = 0; i < littleList.Count; i++)
{
total += littleList[i].getTemperature();
}
mean = total / littleList.Count;
}
public void highTemperatureSort()
{
highTemperature = littleList[0].getTemperature();
for (int i = 0; i < littleList.Count; i++)
{
if (littleList[i].getTemperature() > highTemperature)
{ highTemperature = littleList[i].getTemperature(); }
}
}
public void lowTemperatureSort()
{
lowTemperature = littleList[0].getTemperature();
for (int i = 0; i < littleList.Count; i++)
{
if (littleList[i].getTemperature() < lowTemperature)
{ lowTemperature = littleList[i].getTemperature(); }
}
}
public void dailyReport()
{
Console.WriteLine("\n\n===============NSG Temperature Data Report===============");
Console.WriteLine("\t\t\t\tFahrenheit\tCelcius");
Console.WriteLine("---------------------------------------------------------");
for (int i = 0; i < littleList.Count; i++)
{
Console.WriteLine("Weather Station {0}: \t {1}\t\t {2}", littleList[i].getDesignation(), littleList[i].getTemperature(), ((5 * (littleList[i].getTemperature() - 32)) / 9).ToString("#0.00"));
Console.WriteLine("Contact Person: {0}", littleList[i].getContactName());
Console.WriteLine("---------------------------------------------------------");
}
meanCalculation();
Console.WriteLine("Mean Temperature: \t\t {0}\t\t {1}", mean, ((5 * (mean - 32)) / 9).ToString("#0.00"));
Console.WriteLine("---------------------------------------------------------");
Console.WriteLine("===============End Temperature Data Report===============\n");
}
public void highLowDisplay()
{
Console.WriteLine("\n\n================NSG High-Low Data Report=================");
Console.WriteLine("\t\t\t\tFahrenheit\tCelcius");
Console.WriteLine("Lowest Temperature: \t\t {0}\t\t {1} ", lowTemperature, ((5 * (lowTemperature - 32)) / 9).ToString("#0.00"));
Console.WriteLine("---------------------------------------------------------");
Console.WriteLine("Highest Temperature: \t\t {0}\t\t {1}", highTemperature, ((5 * (highTemperature - 32)) / 9).ToString("#0.00"));
Console.WriteLine("---------------------------------------------------------");
Console.WriteLine("================End High-Low Data Report=================\n");
}
public void highLowReport()
{
highTemperatureSort();
lowTemperatureSort();
highLowDisplay();
}
public void dailyPollenCount()
{
Console.WriteLine("\n\n=================NSG Daily Pollen Report=================");
for (int i = 0; i < littleList.Count; i++)
{
Console.WriteLine("Weather Station {0}: \t {1}", littleList[i].getDesignation(), littleList[i].getPollenCount());
Console.WriteLine("---------------------------------------------------------");
}
Console.WriteLine("=================End Daily Pollen Report=================\n");
}
public void airQuality()
{
Console.WriteLine("\n\n=================NSG Air Quality Report==================");
for (int i = 0; i < littleList.Count; i++)
{
Console.WriteLine("Weather Station {0}: \t {1}", littleList[i].getDesignation(), littleList[i].getAirQuality());
Console.WriteLine("---------------------------------------------------------");
}
Console.WriteLine("=================End Air Quality Report==================\n");
}
public void writeToFile()
{
//Console.Write("Enter a File Name: ");
//fileName = Console.ReadLine();
StreamWriter OutFile = new StreamWriter(fileName);
for (int i = 0; i < littleList.Count; i++)
{
OutFile.WriteLine(littleList[i].getDesignation());
OutFile.WriteLine(littleList[i].getContactName());
OutFile.Write(littleList[i].getTemperature());
OutFile.Write(littleList[i].getPollenCount());
OutFile.Write(littleList[i].getAirQuality());
}
OutFile.Close();
}
public void readFromFile()
{
string designation, contactName;
double temperature, count, score;
//Console.Write("Enter the File Name to read from: ");
//fileName = Console.ReadLine();
StreamReader InFile = new StreamReader(fileName); //Open an object that connects to
//the file for reading...
while(true) //As long as there's content, keep reading
{
if ((designation = InFile.ReadLine()) == null)
break;
contactName = InFile.ReadLine();
temperature = Convert.ToDouble(InFile.ReadLine());
count = Convert.ToDouble(InFile.ReadLine());
score = Convert.ToDouble(InFile.ReadLine());
littleList.Add(new station(designation, contactName, temperature, count, score));
}
InFile.Close(); //Disconnect...
}
}
/* An object of class Nsg stores the station designation and temperature inputs from the keyboard into an array, calculates the mean temperature,
* sorts the highest and lowest temperature, and prints them out into their respective reports.
* */
class Nsg
{
private stationList collection = new stationList();
/* allows the user to designate a name for each disignation
* */
public void addStations()
{
string contactName, designation;
double temp, count, score;
Console.WriteLine("\nEnter Station Information Below, Stop to Quit");
Console.WriteLine("---------------------------------------------------------");
for (; ; )
{
Console.Write("Weather Station Designation: ");
designation = Console.ReadLine();
if (designation.Equals("Stop"))
break;
Console.Write("Contact Person: ");
contactName = Console.ReadLine();
if (contactName.Equals("Stop"))
break;
Console.Write("Enter Temperature: ");
temp = Double.Parse(Console.ReadLine());
if (temp.Equals("Stop"))
break;
Console.Write("Enter Pollen Count: ");
count = Double.Parse(Console.ReadLine());
if (count.Equals("Stop"))
break;
Console.Write("Enter Aire Quiality Index: ");
score = Double.Parse(Console.ReadLine());
if (score.Equals("Stop"))
break;
collection.Add(designation, contactName, temp, count, score);
}
Console.WriteLine("Exiting...");
}
public void dailyReport()
{ collection.dailyReport(); }
public void highLowReport()
{ collection.highLowReport(); }
public void dailyPollenCount()
{ collection.dailyPollenCount(); }
public void dailyAirQuality()
{ collection.airQuality(); }
public void writeToFile()
{ collection.writeToFile(); }
public void readFromFile()
{ collection.readFromFile(); }
}
/* An object of class NsgUI contains the menu
* */
class NsgUI
{
private Nsg projectTwo = new Nsg();
private string command;
/* calls a method depending on the user input
* */
public NsgUI()
{
while (true)
{
menu();
Console.Write("Enter Command: ");
command = Console.ReadLine();
if (command.Equals("8"))
break;
else if (command.Equals("1"))
projectTwo.addStations();
else if (command.Equals("2"))
projectTwo.dailyReport();
else if (command.Equals("3"))
projectTwo.highLowReport();
else if (command.Equals("4"))
projectTwo.dailyPollenCount();
else if (command.Equals("5"))
projectTwo.dailyAirQuality();
else if (command.Equals("6"))
projectTwo.writeToFile();
else if (command.Equals("7"))
projectTwo.readFromFile();
}
}
/* menu display
* */
public static void menu()
{
Console.WriteLine("\nPlease choose one of the following choices (1-5)...");
Console.WriteLine("=========================================================");
Console.WriteLine("\t1. Add Stations");
Console.WriteLine("\t2. Daily Report");
Console.WriteLine("\t3. High-Low Report");
Console.WriteLine("\t4. Daily Pollen Count");
Console.WriteLine("\t5. Daily Air Quality");
Console.WriteLine("\t6. Write To File");
Console.WriteLine("\t7. Read From File");
Console.WriteLine("\t8. Quit");
Console.WriteLine("=========================================================");
}
}
}
I get an error every time i try to access the read from file function. pls help.
Last edited by Teardrop3903; August 4th, 2010 at 12:49 PM.
Reason: Solved
-
August 4th, 2010, 04:35 AM
#2
Re: [Homework] Reading and Writing to file
What's the error you get?
Besides of that, you should Dispose you StreamWriter object adfter closing it: OutFile.Dispose();
Another way would be to use the 'using' syntax:
Code:
using(StreamWriter OutFile = new StreamWriter(fileName)){
//do you stuff
}
Using this syntax, do don't have to call Close() and Dispose() manually.
-
August 4th, 2010, 12:22 PM
#3
Re: [Homework] Reading and Writing to file
heres the error i get
-
August 4th, 2010, 12:48 PM
#4
Re: [Homework] Reading and Writing to file
Haha i feel like an idiot.
I figured out the problem. Turns out i typed in Console.Write() instead of Console.WriteLine() for the double values creating the error.
Anyways thanks for the help.
-
August 4th, 2010, 01:38 PM
#5
Re: [Homework] Reading and Writing to file
Pay attention to the error messages that you get. If you look, the message is very clear:
> Input string was not in the correct format.
You then use the debugger to see what the format was and why it was wrong.
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
|