C# newbie. Read String and Parse.

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2006
Posts: 13
Reputation: chuck577 is an unknown quantity at this point 
Solved Threads: 0
chuck577 chuck577 is offline Offline
Newbie Poster

C# newbie. Read String and Parse.

 
0
  #1
Nov 2nd, 2006
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;
}
}
Last edited by chuck577; Nov 2nd, 2006 at 11:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 46
Reputation: Godfear1 is an unknown quantity at this point 
Solved Threads: 3
Godfear1's Avatar
Godfear1 Godfear1 is offline Offline
Unverified User

Re: C# newbie. Read String and Parse.

 
1
  #2
Nov 3rd, 2006
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.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. namespace ConsoleApplication1
  6. {
  7. class Program
  8. {
  9. private struct DXX
  10. {
  11. public string DXXNumber;
  12. public string DXXName;
  13. }
  14. static void Main(string[] args)
  15. {
  16. ArrayList a = new ArrayList();
  17. 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");
  18. while (!sr.EndOfStream)
  19. {
  20. string s = sr.ReadLine();
  21. char[] splitchars = { '~' };
  22. string[] sarr = s.Split(splitchars);
  23. DXX d = new DXX();
  24. // This is assuming that your dxx number is always first and the name is always the 4th entry.
  25. d.DXXNumber = sarr[0];
  26. d.DXXName = sarr[3];
  27. a.Add(d);
  28. }
  29. sr.Close();
  30. System.Console.WriteLine("Enter the DXX code for the name you want to retrieve." +
  31. " Enter 'done' if your finished searching");
  32. string sin = System.Console.ReadLine();
  33. string name = "";
  34. bool found = false;
  35. while (!sin.Equals("done"))
  36. {
  37. foreach (DXX dl in a)
  38. {
  39. if (dl.DXXNumber.Equals(sin))
  40. {
  41. found = true;
  42. name = dl.DXXName;
  43. break;
  44. }
  45. }
  46. if (found)
  47. {
  48. System.Console.WriteLine("Name: " + name);
  49. found = false;
  50. }
  51. else
  52. System.Console.WriteLine("That entry is not found.");
  53. System.Console.WriteLine("Enter the DXX code for the name you want to retrieve." +
  54. "Enter 'done' if your finished searching");
  55. sin = System.Console.ReadLine();
  56. }
  57. }
  58. }
  59. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 13
Reputation: chuck577 is an unknown quantity at this point 
Solved Threads: 0
chuck577 chuck577 is offline Offline
Newbie Poster

Re: C# newbie. Read String and Parse.

 
0
  #3
Nov 3rd, 2006
Thanks,

I'll try this out!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC