954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C# newbie. Read String and Parse.

Hello,

I asked this question in the c++ forums but I've since moved the small program that I was working on over to C# Forms.

have a current file that I am opening. Each line of said file looks like this:

D40001~10997~811~DANIWEB~555-555-5555~7.70~I~2111

There are around 5000 lines with different DXXXXX numbers. I want the user to input a dnumber (DXXXXX) and have it search the file for said Dnumber and then display the name that is associated with the Dnumber. So typing in 'D40001' should display the name Daniweb. Here is my code but I've went through 5 tutorials on the net and cannot find out how to search this file and do what I want it to do. From the searching that I did do, they said that I need to parse the file and then match the DXXXXX and display the screen. I'm clueless to how to do this. Here is the code that I have so far. Also, this is not for homework. I'm too poor for school. :]


here is the code that I currently have.

namespace DealerNumberSearch
{
partial class Form1
{
///<summary>
/// Required designer variable.
///</summary>
private System.ComponentModel.IContainer components = null;
///<summary>
/// Clean up any resources being used.
///</summary>
///<param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
public void OpenFile()
{
// Try to open the file
try
{
file = new FileStream("dealers.tdl", FileMode.Open, FileAccess.Read);
}
// If file doesnt exist, throw up an error message.
catch (Exception)
{
MessageBox.Show("The file could not be found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
} 
}
public void SearchText()
{
stream = new StreamReader(file);
line = stream.ReadLine();
}
 
#region Windows Form Designer generated code
///<summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///</summary>
private void InitializeComponent()
{
this.goButton = new System.Windows.Forms.Button();
this.getNumber = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
// 
// goButton
// 
this.goButton.Location = new System.Drawing.Point(201, 40);
this.goButton.Name = "goButton";
this.goButton.Size = new System.Drawing.Size(33, 20);
this.goButton.TabIndex = 0;
this.goButton.Text = "GO";
this.goButton.UseVisualStyleBackColor = true;
this.goButton.Click += new System.EventHandler(this.goButton_Click);
// 
// getNumber
// 
this.getNumber.Location = new System.Drawing.Point(57, 40);
this.getNumber.Name = "getNumber";
this.getNumber.Size = new System.Drawing.Size(100, 20);
this.getNumber.TabIndex = 1;
this.getNumber.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
// 
// label1
// 
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(122, 149);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 3;
this.label1.Text = "label1";
// 
// Form1
// 
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.label1);
this.Controls.Add(this.getNumber);
this.Controls.Add(this.goButton);
this.Name = "Form1";
this.Text = "DNS - Dealer Number Search";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private FileStream file;
private StreamReader stream;
private string line;
private string val1;
private System.Windows.Forms.Button goButton;
private System.Windows.Forms.TextBox getNumber;
private System.Windows.Forms.Label label1;
}
}
chuck577
Newbie Poster
15 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Heres a console aplication that basically does what you talking about. Its case sensitive, so when you are prompted for a number you would have to enter D40001 to get the name. This program basically enters the whole file into a arraylist and searches it. It could search your file using less memory (by not saving the information to the arraylist) with a few modifications.

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        private struct DXX
        {
            public string DXXNumber;
            public string DXXName;
        }
        static void Main(string[] args)
        {
            ArrayList a = new ArrayList();
            System.IO.StreamReader sr = new System.IO.StreamReader("C:/Documents and Settings/Jeromy/My Documents/MCSD Self paced training kit/Projects/CodeTextBuilder/CodeTextBuilder/dealers.tdl");
            while (!sr.EndOfStream)
            {
                string s = sr.ReadLine();
                char[] splitchars = { '~' };
                string[] sarr = s.Split(splitchars);
                DXX d = new DXX();
                // This is assuming that your dxx number is always first and the name is always the 4th entry.
                d.DXXNumber = sarr[0];
                d.DXXName = sarr[3];
                a.Add(d);
            }
            sr.Close();
            System.Console.WriteLine("Enter the DXX code for the name you want to retrieve." +
                " Enter 'done' if your finished searching");
            string sin = System.Console.ReadLine();
            string name = "";
            bool found = false;
            while (!sin.Equals("done"))
            {
                foreach (DXX dl in a)
                {
                    if (dl.DXXNumber.Equals(sin))
                    {
                        found = true;
                        name = dl.DXXName;
                        break;
                    }
                }
                if (found)
                {
                    System.Console.WriteLine("Name: " + name);
                    found = false;
                }
                else
                    System.Console.WriteLine("That entry is not found.");
                System.Console.WriteLine("Enter the DXX code for the name you want to retrieve." +
                    "Enter 'done' if your finished searching");
                sin = System.Console.ReadLine();
            }
        }
    }
}
Godfear1
Light Poster
47 posts since Oct 2006
Reputation Points: 12
Solved Threads: 3
 

Thanks,

I'll try this out!

chuck577
Newbie Poster
15 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

sir how do you parse strings inputted by line? then it will be read according to this pattern in C# (Visual Studio)
for example:

Input| Token Pattern
Begin | reservedword

int x,y; | datatype variable comma variable semicolon

End | reservedword

how do you parse it? :( and then the whitespaces in input will be deleted?

kei1412
Newbie Poster
7 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

Hi kei1412, welcome!
You should have posted your question in a new thread.
I think you want more something like this: http://www.daniweb.com/forums/forum61.html
Please look up the two other parts in the code snippet section.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

Sorry, made a mistake
The correct link to the snippet is: http://www.daniweb.com/code/snippet217185.html

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

sir i received an error :( it says: "dispose (bool) no suitable method found to overwrite" it is in the designer.cs how to cure that error :( mmm... thank you for the reply.. how could i correct it.. thank you very much again :)

kei1412
Newbie Poster
7 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

another error saying
"An object reference is required for the non-static field, method or property" :( mmmm.... :( cant solve it :(

kei1412
Newbie Poster
7 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

public static String Tokenizer(string s)
{
int num;
string Tokenizer;
Tokenizer = "";
SQLOpen();
SQLCommand = new OleDbCommand("SELECT Category, SubCat FROM Combat_Weapons WHERE Word = '"+ s.ToUpper() +"'", SQLConnection );
SQLReader = SQLCommand.ExecuteReader();
if ((SQLReader.Item(0) = "c") | (SQLReader.Item(0) = "w"))
Tokenizer = SQLReader.Item(0);
else
{
if (SQLReader.RecordsAffected = 0)
{
SQLCommand = new OleDbCommand("SELECT SpecialSymbols, SSName FROM Attributes WHERE Sym = '" + s + "'", SQLConnection);
SQLReader = SQLCommand.ExecuteReader;
Tokenizer = "a," + SQLReader.Item(0) + "," + SQLReader.Item(1);
if (SQLReader.RecordsAffected = 0)
{
if (!int.TryParse(s, out num))
Tokenizer = "w, var";
else
{
Tokenizer = "s";
}
}
}
}
SQLClose();
return Tokenizer;
}

this is my program in calling a database but it has many errors.. i couldnt understand the problem of the program :( :(

kei1412
Newbie Poster
7 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

Please read the forum rules and keep things tidy.
First, you ressurected a 4 year dead thread to ask a loosely related question and now you've posted a completely unrelated question in the same thread.
Please start a new thread unless you have something relevant to add to the current one.
Second, please place code inside [CODE][/CODE] tags as this maintains the formatting and makes it much easier to read.

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You