Input string was not in correct format
- by Luke
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace measurementConverter
{
class Program
{
static void Main(string[] args)
{
//read in the file
StreamReader convert = new StreamReader("../../convert.txt");
//define variables
string line = convert.ReadLine();
int conversion;
int numberIn;
float conversionFactor;
Console.WriteLine("Enter the conversion in the form (amount,from,to)");
String inputMeasurement = Console.ReadLine();
string[] inputMeasurementArray = inputMeasurement.Split(',');
while (line != null)
{
string[] fileMeasurementArray = line.Split(',');
if (fileMeasurementArray[0] == inputMeasurementArray[1])
{
if (fileMeasurementArray[1] == inputMeasurementArray[2])
{
Console.WriteLine("{0}", fileMeasurementArray[2]);
}
}
line = convert.ReadLine();
//convert to int
numberIn = Convert.ToInt32(inputMeasurementArray[0]);
conversionFactor = Convert.ToInt32(fileMeasurementArray[2]);
conversion = (numberIn * conversionFactor);
}
Console.ReadKey();
}
}
}
Hello, I am trying to get the calculating going.
On the line conversionFactor = Convert.ToInt32(fileMeasurementArray[2]);, I am getting an error saying "Input string was not in correct format". Please help!
The text file consists of the following:
ounce,gram,28.0
pound,ounce,16.0
pound,kilogram,0.454
pint,litre,0.568
inch,centimetre,2.5
mile,inch,63360.0