944,221 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 38298
  • C# RSS
Nov 2nd, 2006
1

C# newbie. Read String and Parse.

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chuck577 is offline Offline
15 posts
since Oct 2006
Nov 3rd, 2006
2

Re: C# newbie. Read String and Parse.

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. }
Reputation Points: 12
Solved Threads: 3
Unverified User
Godfear1 is offline Offline
46 posts
since Oct 2006
Nov 3rd, 2006
0

Re: C# newbie. Read String and Parse.

Thanks,

I'll try this out!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chuck577 is offline Offline
15 posts
since Oct 2006
Aug 6th, 2010
-1

parse by line

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kei1412 is offline Offline
7 posts
since Aug 2010
Aug 6th, 2010
1
Re: C# newbie. Read String and Parse.
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.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Aug 7th, 2010
0

Oops

Sorry, made a mistake
The correct link to the snippet is: http://www.daniweb.com/code/snippet217185.html
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Aug 23rd, 2010
0

error in c#

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kei1412 is offline Offline
7 posts
since Aug 2010
Aug 23rd, 2010
0
Re: C# newbie. Read String and Parse.
another error saying
"An object reference is required for the non-static field, method or property" mmmm.... cant solve it
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kei1412 is offline Offline
7 posts
since Aug 2010
Aug 23rd, 2010
0
Re: C# newbie. Read String and Parse.
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kei1412 is offline Offline
7 posts
since Aug 2010
Aug 24th, 2010
0
Re: C# newbie. Read String and Parse.
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.
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Select image in panel
Next Thread in C# Forum Timeline: app written on xp crashes on vista





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC