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();
}
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
According to the line of the text file, fileMeasurementArray[2]
will be 28.0 pound
. This cannot be converted to an int. You should remove the string and keep only the number 28.0
.
UPDATE : the question was updated and the line of text file corrected. So my previous answer does'nt make sense anymore.
The problem occurs probably because you are trying to convert a float to an int.
You are trying to convert a float to an int.
Define conversionFactor as a double
also the variable conversion needs to be a double aswell.
like so
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();
double conversion;
int numberIn;
double 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.ToDouble(fileMeasurementArray[2]);
conversion = (numberIn * conversionFactor);
}
Console.ReadKey();
}
}
}
'28.0 pound' isn't an integer. I'd imagine that's where your problem lies.
numberIn
and conversionFactor
are floats, not Ints. Use:
float numberIn;
float conversionFactor;
...
//convert to float
numberIn = Convert.ToSingle(inputMeasurementArray[0]);
conversionFactor = Convert.ToSingle(fileMeasurementArray[2]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.