473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Simple Unit Converter Reading from text file

Hi,

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

ounce,gram,28.0
pound,ounce,16.0
...

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.

Expand|Select|Wrap|Line Numbers
  1.         {
  2.             const char DELIM1 = '\r';
  3.             string[] split1;
  4.  
  5.             int counter = 0;
  6.             string line;
  7.  
  8.             System.IO.StreamReader filereader = new System.IO.StreamReader("C:/convert.txt");
  9.             while ((line = filereader.ReadLine()) != null)
  10.             {
  11.                 split1 = line.Split(DELIM1);
  12.                 Console.WriteLine(split1[0]);
  13.                 counter++;
  14.             }
  15.             filereader.Close();
  16.             Console.ReadLine();
  17.         }
Mar 9 '11 #1
1 1950
GaryTexmo
1,501 Expert 1GB
I'll be honest, this sounds like a homework assignment so I'm going to give you some general ideas here and you'll have to figure the rest out for yourself :)

I'm not sure if your file reading part is correct. You're reading each line, which is correct, but then you're splitting that line on '\r' which is part of a line separator (Environment.Newline, for US-EN is '\r\n' I think).

Since you already have each line, you might want to split it by a ',' character instead, this will give you each token on the line in a string array. The first element will be the first unit, the second element will be the second unit, and the third element will be the value.

Now you need to find a way to map the conversion. You can do this any way you like... one such way might be to use a Hashtable where the key is something that represents the unit combination. The value could be a single factor, or if you need something more elaborate, a delegate to a conversion method.

That's just one way, I'm sure there are many other ways to solve this. Sit down and brainstorm how you want to approach the problem and what you want to do.

Give it a try and if you run into any coding problems, feel free to post back here and we'll try to help you work through them.
Mar 9 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
14
by: Job Lot | last post by:
I have tab delimited text file which gets populated on daily basis via automated process. New entry is written at the bottom. I need to create a utility which makes a copy of this file with 10 most...
8
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a...
7
by: Drew Berkemeyer | last post by:
Hello, I'm using the following code to read a text file in VB.NET. Dim sr As StreamReader = File.OpenText(strFilePath) Dim input As String = sr.ReadLine() While Not input Is Nothing...
4
by: Nina | last post by:
Hi everyone, Do you know why the following code only read certain number of lines of text file, but not the entire file? Dim sr As StreamReader Dim str As String Dim al As ArrayList = New...
4
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
1
by: John | last post by:
I have a process that reads a text file then uploads the data into a database table. The text file has 10 lines at the end of the file that are blank BUT it appears that the enter key or space bar...
3
by: John | last post by:
How can I tell if a line begins with a number instead of a character? I need to read a text file and some of the data begins with a character which is causing me an issue in reading and uploading...
3
by: jasvinder singh | last post by:
Respected Sir/madam, Can you help in providing code in 'C' for Reading text file with n number of rows and columns and putting the result in arrays.The sample file is as follows: rim_label =...
1
by: Hugo | last post by:
Hi: I need to read a simple txt file where there are some variables. I've done this a lot of times but in this case I have something different: the text file is located at an ftp server. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.