Could anyone help with the code I have made. The problem is that it is reading in the conversion txt file but because there is a space between some of the data it will not display the result, it comes up with a message saying 'out of bounds of the array'. The idea of the code is that it uses the file to convert a users input. Ex. 5,ounce,gram this would display a result like 5 ounces is equal to 141 grams. But for example it won't display the result for 1,mile,inch because of the space. I also cannot change the txt file. Cheers.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Final_version_2
{
class Program
{
static void Main(string[] args)
{
string line, To, From, j, l, lineTotal = "";// These are the strings for the program, which includes the line total to count the amount of lines in the text file.
double Factor, Output, Amount; //These are the double varibles which will be floating point number varibles.
string[] SplitData = new string[2];// This is setting up the array for the program.
string[] fileLn; // This is another srting called fileLn.
StreamReader Units = new StreamReader(@"..convert.txt"); // This is file path to find the convert.txt file.
while ((line = Units.ReadLine()) != null) // This is the start of the while loop.
{
lineTotal += line + "\n"; // This is count the total amount of lines in txt file.
}
fileLn = lineTotal.Split('\n'); //This places the file lines in an array of string.
Main:
//This is getting the inputs from the user.
Console.WriteLine("Please input the amount, to and from type (Ex. 5,ounces,grams):"); // This is displayed on the console for the user to read.
string Input = Console.ReadLine(); // This puts the inputs as strings.
for (int i = 0; i < fileLn.Length - 1; i++)
{
SplitData = fileLn[i].Split(',');
To = SplitData[0];
From = SplitData[1];
Factor = Convert.ToDouble(SplitData[2]);
Console.WriteLine(To + " - " + From + " - " + Factor + " - ");
string[] Measurements = Input.Split(',', ' ', '/', '.');
Amount = Convert.ToDouble(Measurements[0]);
j = Measurements[1];
l = Measurements[2];
if (j == To)
{
Output = (Factor * Amount);
Console.WriteLine("{0} {1} is equal to {2} {3}", Amount, Measurements[1], Output, Measurements[2]);
Console.ReadLine();
goto Main;
//break;
}
else
{
// Console.WriteLine("Not Matched");
// Console.ReadLine();
}
}
Units.Close();
}
}
}