Tyler Merrill Tyler Merrill - 11 months ago 49
C# Question

Need Help Pulling Data From A File

I'm learning how to read and write to external text files with C#(yes, this is classwork, getting that out of the way now), and I've kind of hit a wall. See, the first part of this assignment was to create a program that can write customer records to an external file, after creating a Customer class with ID number, name, and current balance fields, which I did.

My predicament is that I now have to create a program to search the file I created with that first one, and print out each line to the console with a balance greater than or equal to a minimum balance supplied by a user. I'm drawing a blank on how to actually do this.

This is as far as I've gotten:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using System.IO;

namespace FindCustomerRecords2
class Program
static void Main(string[] args)
//Intialize variables
double minBal;
string line;

//Prompt user to enter minimum balance and store in minBal
Write("Please enter a minimum balance to pull up\n" +
"all customers that owe at least that much.");
minBal = Convert.ToDouble(ReadLine());

//Open data stream to CustomerRecords.txt
StreamReader file = new StreamReader(@"H:\C#\Visual C# 2015\Ch. 14 - Files and Streams\CustomerRecords.txt");
//Loop through CustomerRecords.txt, find values
//greater than or euqal to minBal, and display
//each line with such a value
while((line = file.ReadLine()) != null)


Now, I'm not looking to be fed an answer, I just genuinely don't know what to use to accomplish what I've been asked to do. Anyone willing to help me out?

EDIT: I eventually found out what I was doing wrong. For the future reference of anyone who may have the same problem in the future: I got it to work by using Split() to assign the data to an array called fields, then assigned each array position of fields to variables called id, name, and balance, and compared each balance to minBal in an if statement like so:

fields = recordIn.Split(DELIM);
id = Convert.ToInt32(fields[0]);
name = fields[1];
balance = Convert.ToDouble(fields[2]);

Thanks to everyone who replied.

Answer Source

Take a closer look inside of your while loop:

while((line = file.ReadLine()) != null)


at this point inside of the loop, you should have the following data:

45454, Glenn Matthews, 54.87

If not, then you created your CSV file incorrectly. Let's assume for now that you created it correctly. How would you go about getting the value out of your line variable in order to compare it to your minBal variable? If you can figure that part out then you can probably solve the rest on your own.

What tools (such as string methods) do you have at your disposal that could help do this?