Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 738357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:49:38+00:00 2026-05-14T07:49:38+00:00

while (true) { //read in the file StreamReader convert = new StreamReader(../../convert.txt); //define variables

  • 0
while (true)
{
    //read in the file
    StreamReader convert = new StreamReader("../../convert.txt");

    //define variables
    string line = convert.ReadLine();
    double conversion;
    int numberIn;
    double conversionFactor;

    //ask for the conversion information
    Console.WriteLine("Enter the conversion in the form (Amount, Convert from, Convert to)");
    String inputMeasurement = Console.ReadLine();
    string[] inputMeasurementArray = inputMeasurement.Split(',');


    //loop through the lines looking for a match
    while (line != null)
    {
        string[] fileMeasurementArray = line.Split(',');
        if (fileMeasurementArray[0] == inputMeasurementArray[1])
        {
            if (fileMeasurementArray[1] == inputMeasurementArray[2])
            {
                Console.WriteLine("The conversion factor for {0} to {1} is {2}", inputMeasurementArray[1], inputMeasurementArray[2], fileMeasurementArray[2]);

                //convert to int
                numberIn = Convert.ToInt32(inputMeasurementArray[0]);
                conversionFactor = Convert.ToDouble(fileMeasurementArray[2]);

                conversion = (numberIn * conversionFactor);
                Console.WriteLine("{0} {1} is {2} {3} \n", inputMeasurementArray[0], inputMeasurementArray[1], conversion, inputMeasurementArray[2]);
                break;
            }
        }
        else
        {
            Console.WriteLine("Please enter two valid conversion types \n");
            break;
        }
        line = convert.ReadLine();
    }
}

The 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

The user will input something like 6,ounce,gram

The idea is that it finds the correct line by checking if the first and second words in the file are the same as the second and third the user enters.

The problem is that if it checks the first line and it fails the if statement, if goes through to the else statement and stops. I am trying to find a way where it will stop after the it finds the correct line but not until. If someone types in a value that isn’t in the file, then it should show an error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:49:38+00:00Added an answer on May 14, 2026 at 7:49 am

    Remove the break statement in the else clause. That’s causing it to exit the loop.

    You could really improve this code by reading the conversion factors into an internal data structure, perhaps a dictionary keyed by the “from” conversion unit with the value be a dictionary of possible output units and their conversion factor — or a custom key/value pair if you only have a single possible output unit. This would turn your inner loop into a two-stage look up (much faster) and save you the trouble of having to re-read the conversion file each time. As @Ben points out, the error message is in the wrong place, too. It needs to be outside the loop/lookup, and only executed if no match was found.

    Sample code — note there is no input validation in this code:

    var conversions = new Dictionary<string,Dictionary<string,double>>();
    var convert = new StreamReader("../../convert.txt");
    while ((var line = convert.ReadLine()) != null)
    {
        string components = line.Split(',');
        Dictionary<string,double> unitConversions;
        if (conversions.ContainsKey( components[0] ))
        {
            unitConversions = conversions[components[0]];
        }
        else
        {
            unitConversions = new Dictionary<string,double>();
            conversions.Add( components[0], unitConversions );
        }
        unitConversions.Add( components[1], Convert.ToDouble( components[2] ) );
    }
    
    while (true)
    {
        //ask for the conversion information     
        Console.WriteLine("Enter the conversion in the form (Amount, Convert from, Convert to)");     
        var inputMeasurement = Console.ReadLine();     
        var inputMeasurementArray = inputMeasurement.Split(',');
    
        bool converted = false;
        Dictionary<string,double> unitConversion;
        if (conversions.TryGetValue(  inputMeasurementArray[1], out unitConversion ))
        {
             double conversionFactor;
             if (unitConversion.TryGetValue( inputMeasurementArray[2], out conversionFactor ))
             {
                 converted = true;
                 conversion = Convert.ToDouble( inputMeasurementArray[0] ) * conversionFactor;
                 Console.WriteLine("{0} {1} is {2} {3} \n", inputMeasurementArray[0], inputMeasurementArray[1], conversion, inputMeasurementArray[2]);              
             }
        }
    
        if (!converted)
        {
             Console.WriteLine("Please enter two valid conversion types\n");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following basic code to read a text file from a StreamReader:
I want to read a text file line by line. I wanted to know
The Python docs on file.read() state that An empty string is returned when EOF
I'm trying to monitor the file content and adding any new line to JTextArea.
I want to read words in a text file of a line separated by
from random import * while True: random1 = randint(1,20) random2 = randint(1,20) print(h =
try { if (myBoolean) { while (true) ; } else { System.exit(1); } }
Consider the following code: while(true) { someFunction(); Thread.sleep(1000); } What I want is that,
As of now I'm using a while(true) method to detect changes in memory. The
here is my script: from random import * from turtle import * while True:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.