I am trying to create a simple unit converter which reads in the units from a file the file is layout like this:


It is read line by line like, 1 ounce is equal to 28 grams.

How would i go about comparing what the users inputs for to and from values and finding them in the array and * them by the correct factor.

Here is the code snippet of reading from the file.

            const char DELIM1 = '\r';
            string[] split1;

            int counter = 0;
            string line;

            System.IO.StreamReader filereader = new System.IO.StreamReader("C:convert.txt");
            while ((line = filereader.ReadLine()) != null)
                split1 = line.Split(DELIM1);

Recommended Answers

All 3 Replies

You are on the right track, except that your delimiter for the split function should be a comma ',' rather than a carriage return character '\r'. Once you have done the split, you will have three values in your split1 array - from unit, to unit and multiplier value. I would create a class to store these values and keep a list of them. Then search your list from the user input to work out which conversion to perform.

This might be beyond your current abilities but I'd keep a Dictionary<String, Dictionary<String, double>> of the values. The first key would be the from units, the 2nd key the to units and the value the conversion rate.

You could then use Dictionary.Keys to populate a dropdown list of From units. When the user selects one you'd get the Value for that key (Which is another Dictionary) and use the Keys from it to populate a 2nd dropdown box. Users selects the To units, enters a value and presses a button. You do the conversion.

Member Avatar

that is out of his current abilities so i will suggest once you have the three values (convertFrom, converTo and convertRate) put them into 3 separate arraylists.

then once you have read in the user's input and split it (or selected it from a combo box) search the first arraylist for the first conversion and if the second arraylist at that index equals the second conversion then times the users amount by the conversionRate in the 3rd arraylist.

Hope that helps!! =D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.